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