diff --git a/docs/rules/test-title.md b/docs/rules/test-title.md index af22ccca..82b29eb7 100644 --- a/docs/rules/test-title.md +++ b/docs/rules/test-title.md @@ -2,24 +2,12 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/test-title.md) -Tests should have a title. +Tests should have a title. AVA [v1.0.1](https://github.com/avajs/ava/releases/tag/v1.0.1) and later enforces this at runtime. ## Fail ```js -/*eslint ava/test-title: ["error", "if-multiple"]*/ -import test from 'ava'; - -test(t => { - t.pass(); -}); - -test(t => { - t.pass(); -}); - -/*eslint ava/test-title: ["error", "always"]*/ import test from 'ava'; test(t => { @@ -31,31 +19,9 @@ test(t => { ## Pass ```js -/*eslint ava/test-title: ["error", "if-multiple"]*/ -import test from 'ava'; - -test(t => { - t.pass(); -}); - -test('foo', t => { - t.pass(); -}); - -/*eslint ava/test-title: ["error", "always"]*/ import test from 'ava'; test('foo', t => { t.pass(); }); ``` - -## Options - -The rule takes one option, a string, which could be either `"always"` or `"if-multiple"`. The default is `"if-multiple"`. If the option is set to `"if-multiple"`, the rule will only trigger if there are multiple tests in a file. - -You can set the option in configuration like this: - -```js -"ava/test-title": ["error", "always"] -``` diff --git a/rules/test-title.js b/rules/test-title.js index 9e4617b8..d1b87a39 100644 --- a/rules/test-title.js +++ b/rules/test-title.js @@ -5,8 +5,6 @@ const util = require('../util'); const create = context => { const ava = createAvaRule(); - const ifMultiple = context.options[0] !== 'always'; - let testCount = 0; return ava.merge({ CallExpression: visitIf([ @@ -14,38 +12,24 @@ const create = context => { ava.isTestNode, ava.hasNoHookModifier ])(node => { - testCount++; + const firstArgumentIsFunction = node.arguments.length < 1 || util.isFunctionExpression(node.arguments[0]); - const requiredLength = ava.hasTestModifier('todo') ? 1 : 2; - const hasNoTitle = node.arguments.length < requiredLength; - const isOverThreshold = !ifMultiple || testCount > 1; - - if (hasNoTitle && isOverThreshold) { + if (firstArgumentIsFunction) { context.report({ node, message: 'Test should have a title.' }); } - }), - 'Program:exit': () => { - testCount = 0; - } + }) }); }; -const schema = [{ - enum: [ - 'always', - 'if-multiple' - ] -}]; - module.exports = { create, meta: { + type: 'problem', docs: { url: util.getDocsUrl(__filename) - }, - schema + } } }; diff --git a/test/test-title.js b/test/test-title.js index b1c1d66b..5e361ad4 100644 --- a/test/test-title.js +++ b/test/test-title.js @@ -13,99 +13,49 @@ const header = 'const test = require(\'ava\');\n'; ruleTester.run('test-title', rule, { valid: [ - // Default options should be `['if-multiple']` - header + 'test(t => { t.pass(); t.end(); });', - { - code: header + 'test("my test name", t => { t.pass(); t.end(); });', - options: ['always'] - }, - { - code: header + 'test(`my test name`, t => { t.pass(); t.end(); });', - options: ['always'] - }, - { - code: header + 'test(\'my test name\', t => { t.pass(); t.end(); });', - options: ['always'] - }, - { - code: header + 'test.cb("my test name", t => { t.pass(); t.end(); });', - options: ['always'] - }, - { - code: header + 'test.todo("my test name");', - options: ['always'] - }, - { - code: header + 'test.before(t => {});', - options: ['always'] - }, - { - code: header + 'test.after(t => {});', - options: ['always'] - }, - { - code: header + 'test.beforeEach(t => {});', - options: ['always'] - }, - { - code: header + 'test.afterEach(t => {});', - options: ['always'] - }, - { - code: header + 'test.cb.before(t => {}); test.before.cb(t => {});', - options: ['always'] - }, - { - code: header + 'test(t => { t.pass(); t.end(); });', - options: ['if-multiple'] - }, - { - code: header + 'notTest(t => { t.pass(); t.end(); });', - options: ['always'] - }, - { - code: header + 'test(macroFn, arg1, arg2);', - options: ['always'] - }, + header + 'test("my test name", t => { t.pass(); t.end(); });', + header + 'test(`my test name`, t => { t.pass(); t.end(); });', + header + 'test(\'my test name\', t => { t.pass(); t.end(); });', + header + 'test.cb("my test name", t => { t.pass(); t.end(); });', + header + 'test.todo("my test name");', + header + 'test.before(t => {});', + header + 'test.after(t => {});', + header + 'test.beforeEach(t => {});', + header + 'test.afterEach(t => {});', + header + 'test.cb.before(t => {}); test.before.cb(t => {});', + header + 'notTest(t => { t.pass(); t.end(); });', + header + 'test([], arg1, arg2);', + header + 'test({}, arg1, arg2);', // Shouldn't be triggered since it's not a test file - { - code: 'test(t => {});', - options: ['always'] - } + 'test(t => {});' ], invalid: [ { - code: header + 'test(t => {}); test(t => {});', + code: header + 'test(t => {});', + errors + }, + { + code: header + 'test(t => {}, "my test name");', errors }, { code: header + 'test(t => { t.pass(); t.end(); });', - options: ['always'], errors }, { code: header + 'test.cb(t => { t.pass(); t.end(); });', - options: ['always'], errors }, { code: header + 'test.cb.skip(t => { t.pass(); t.end(); });', - options: ['always'], errors }, { code: header + 'test(t => { t.pass(); t.end(); });', - options: ['always'], errors }, { code: header + 'test.todo();', - options: ['always'], - errors - }, - { - code: header + 'test(t => {}); test(t => {});', - options: ['if-multiple'], errors } ]