Skip to content

Commit 5bb0a4b

Browse files
author
Guillaume Martigny
committed
test-title only errored when first argument is a function
fix avajs#185
1 parent c5a77bb commit 5bb0a4b

File tree

2 files changed

+28
-90
lines changed

2 files changed

+28
-90
lines changed

rules/test-title.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,35 @@ const util = require('../util');
55

66
const create = context => {
77
const ava = createAvaRule();
8-
const ifMultiple = context.options[0] !== 'always';
9-
let testCount = 0;
8+
const functionTypes = [
9+
'FunctionDeclaration',
10+
'FunctionExpression',
11+
'ArrowFunctionExpression'
12+
];
1013

1114
return ava.merge({
1215
CallExpression: visitIf([
1316
ava.isInTestFile,
1417
ava.isTestNode,
1518
ava.hasNoHookModifier
1619
])(node => {
17-
testCount++;
20+
const firstArgumentIsFunction = node.arguments.length < 1 || functionTypes.includes(node.arguments[0].type);
1821

19-
const requiredLength = ava.hasTestModifier('todo') ? 1 : 2;
20-
const hasNoTitle = node.arguments.length < requiredLength;
21-
const isOverThreshold = !ifMultiple || testCount > 1;
22-
23-
if (hasNoTitle && isOverThreshold) {
22+
if (firstArgumentIsFunction) {
2423
context.report({
2524
node,
2625
message: 'Test should have a title.'
2726
});
2827
}
29-
}),
30-
'Program:exit': () => {
31-
testCount = 0;
32-
}
28+
})
3329
});
3430
};
3531

36-
const schema = [{
37-
enum: [
38-
'always',
39-
'if-multiple'
40-
]
41-
}];
42-
4332
module.exports = {
4433
create,
4534
meta: {
4635
docs: {
4736
url: util.getDocsUrl(__filename)
48-
},
49-
schema
37+
}
5038
}
5139
};

test/test-title.js

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,99 +13,49 @@ const header = 'const test = require(\'ava\');\n';
1313

1414
ruleTester.run('test-title', rule, {
1515
valid: [
16-
// Default options should be `['if-multiple']`
17-
header + 'test(t => { t.pass(); t.end(); });',
18-
{
19-
code: header + 'test("my test name", t => { t.pass(); t.end(); });',
20-
options: ['always']
21-
},
22-
{
23-
code: header + 'test(`my test name`, t => { t.pass(); t.end(); });',
24-
options: ['always']
25-
},
26-
{
27-
code: header + 'test(\'my test name\', t => { t.pass(); t.end(); });',
28-
options: ['always']
29-
},
30-
{
31-
code: header + 'test.cb("my test name", t => { t.pass(); t.end(); });',
32-
options: ['always']
33-
},
34-
{
35-
code: header + 'test.todo("my test name");',
36-
options: ['always']
37-
},
38-
{
39-
code: header + 'test.before(t => {});',
40-
options: ['always']
41-
},
42-
{
43-
code: header + 'test.after(t => {});',
44-
options: ['always']
45-
},
46-
{
47-
code: header + 'test.beforeEach(t => {});',
48-
options: ['always']
49-
},
50-
{
51-
code: header + 'test.afterEach(t => {});',
52-
options: ['always']
53-
},
54-
{
55-
code: header + 'test.cb.before(t => {}); test.before.cb(t => {});',
56-
options: ['always']
57-
},
58-
{
59-
code: header + 'test(t => { t.pass(); t.end(); });',
60-
options: ['if-multiple']
61-
},
62-
{
63-
code: header + 'notTest(t => { t.pass(); t.end(); });',
64-
options: ['always']
65-
},
66-
{
67-
code: header + 'test(macroFn, arg1, arg2);',
68-
options: ['always']
69-
},
16+
header + 'test("my test name", t => { t.pass(); t.end(); });',
17+
header + 'test(`my test name`, t => { t.pass(); t.end(); });',
18+
header + 'test(\'my test name\', t => { t.pass(); t.end(); });',
19+
header + 'test.cb("my test name", t => { t.pass(); t.end(); });',
20+
header + 'test.todo("my test name");',
21+
header + 'test.before(t => {});',
22+
header + 'test.after(t => {});',
23+
header + 'test.beforeEach(t => {});',
24+
header + 'test.afterEach(t => {});',
25+
header + 'test.cb.before(t => {}); test.before.cb(t => {});',
26+
header + 'notTest(t => { t.pass(); t.end(); });',
27+
header + 'test([], arg1, arg2);',
28+
header + 'test({}, arg1, arg2);',
7029
// Shouldn't be triggered since it's not a test file
71-
{
72-
code: 'test(t => {});',
73-
options: ['always']
74-
}
30+
'test(t => {});'
7531
],
7632
invalid: [
7733
{
78-
code: header + 'test(t => {}); test(t => {});',
34+
code: header + 'test(t => {});',
35+
errors
36+
},
37+
{
38+
code: header + 'test(t => {}, "my test name");',
7939
errors
8040
},
8141
{
8242
code: header + 'test(t => { t.pass(); t.end(); });',
83-
options: ['always'],
8443
errors
8544
},
8645
{
8746
code: header + 'test.cb(t => { t.pass(); t.end(); });',
88-
options: ['always'],
8947
errors
9048
},
9149
{
9250
code: header + 'test.cb.skip(t => { t.pass(); t.end(); });',
93-
options: ['always'],
9451
errors
9552
},
9653
{
9754
code: header + 'test(t => { t.pass(); t.end(); });',
98-
options: ['always'],
9955
errors
10056
},
10157
{
10258
code: header + 'test.todo();',
103-
options: ['always'],
104-
errors
105-
},
106-
{
107-
code: header + 'test(t => {}); test(t => {});',
108-
options: ['if-multiple'],
10959
errors
11060
}
11161
]

0 commit comments

Comments
 (0)