-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (45 loc) · 1.36 KB
/
index.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
module.exports = {
rules: {
'jest-root-describe': {
meta: {
fixable: "code"
},
create(context) {
const absoluteFileName = context.getFilename();
if (!absoluteFileName.endsWith('.spec.ts')) {
return {};
}
const relativeFileName = absoluteFileName
.replace(process.cwd(), '')
.replace(/\\/g, '/')
.replace(/^(?:\/(?:lib|src|test))?\//, '');
const testName = String(relativeFileName.replace(/\.spec\.ts$/, ''));
return {
CallExpression(node) {
const { callee } = node;
if (
callee.type === 'Identifier' &&
callee.name === 'describe' &&
node.parent.parent.type === 'Program'
) {
const [descr] = node.arguments;
const isOkay =
descr.type === 'Literal' &&
typeof descr.value === 'string' &&
testName === descr.value;
if (!isOkay) {
context.report({
node: descr,
message: `Test must be described by this string: '${testName}'`,
fix(fixer) {
return fixer.replaceText(descr, `'${testName}'`);
},
});
}
}
},
};
},
},
},
};