-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtest-title-format.js
61 lines (54 loc) · 1.17 KB
/
test-title-format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const {visitIf} = require('enhance-visitors');
const createAvaRule = require('../create-ava-rule');
const util = require('../util');
const create = context => {
const ava = createAvaRule();
let titleRegExp;
if (context.options[0]?.format) {
titleRegExp = new RegExp(context.options[0].format);
} else {
return {};
}
return ava.merge({
CallExpression: visitIf([
ava.isInTestFile,
ava.isTestNode,
ava.hasNoUtilityModifier,
])(node => {
const requiredLength = ava.hasTestModifier('todo') ? 1 : 2;
const hasTitle = node.arguments.length >= requiredLength;
if (hasTitle) {
const title = node.arguments[0];
if (title.type === 'Literal' && !titleRegExp.test(title.value)) {
context.report({
node,
message: `The test title doesn't match the required format: \`${titleRegExp}\`.`,
});
}
}
}),
});
};
const schema = [
{
type: 'object',
properties: {
format: {
type: 'string',
default: undefined,
},
},
},
];
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Ensure test titles have a certain format.',
url: util.getDocsUrl(__filename),
},
schema,
},
};