Skip to content

Commit df6374a

Browse files
authored
Rely on AVA to provide a helper for our rules
This impacts no-ignored-test-files and no-import-test-files. These rules now assume an AVA version is installed that provides a helper. This helper can classify files according to AVA's configuration. This removes the need to specify options for these rules.
1 parent a184661 commit df6374a

30 files changed

+131
-621
lines changed

docs/rules/no-ignored-test-files.md

+6-62
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,22 @@
22

33
Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/related/eslint-plugin-ava/docs/rules/no-ignored-test-files.md)
44

5-
When searching for tests, AVA ignores files contained in `node_modules` or folders named `fixtures` or `helpers`. By default, it will search in `test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js`, which you can override by specifying a path when launching AVA or in the [AVA configuration in the `package.json` or `ava.config.js` files](https://github.com/avajs/ava/blob/master/docs/06-configuration.md).
5+
This rule will verify that files which create tests are treated as test files by AVA. It will consider the root of the project to be the closest folder containing a `package.json` file, and will not do anything if it can't find one. Test files in `node_modules` will not be linted as they are ignored by ESLint.
66

7-
This rule will verify that files which create tests are in the searched files and not in ignored folders. It will consider the root of the project to be the closest folder containing a `package.json` file, and will not do anything if it can't find one. Test files in `node_modules` will not be linted as they are ignored by ESLint.
8-
9-
Note that this rule will not be able to warn correctly if you use AVA by specifying the files in the command line ( `ava "lib/**/*.test.js"` ). Prefer configuring AVA as described in the link above.
107

118
## Fail
129

1310
```js
14-
// File: test/foo/fixtures/bar.js
15-
// Invalid because in `fixtures` folder
16-
import test from 'ava';
17-
18-
test('foo', t => {
19-
t.pass();
20-
});
21-
22-
// File: test/foo/helpers/bar.js
23-
// Invalid because in `helpers` folder
11+
// File: test/_helper.js
12+
// Invalid because a helper.
2413
import test from 'ava';
2514

2615
test('foo', t => {
2716
t.pass();
2817
});
2918

30-
// File: lib/foo.test.js
31-
// Invalid because not in the searched files
32-
import test from 'ava';
33-
34-
test('foo', t => {
35-
t.pass();
36-
});
37-
38-
// File: test.js
39-
// with { "files": ["lib/**/*.test.js", "utils/**/*.test.js"] }
40-
// in either `package.json` under 'ava key' or in the rule options
41-
// Invalid because not in the searched files
19+
// File: lib/foo.js
20+
// Invalid because not a test file.
4221
import test from 'ava';
4322

4423
test('foo', t => {
@@ -50,45 +29,10 @@ test('foo', t => {
5029
## Pass
5130

5231
```js
53-
// File: test/foo/not-fixtures/bar.js
54-
import test from 'ava';
55-
56-
test('foo', t => {
57-
t.pass();
58-
});
59-
60-
// File: test/foo/not-helpers/bar.js
32+
// File: test/foo.js
6133
import test from 'ava';
6234

6335
test('foo', t => {
6436
t.pass();
6537
});
66-
67-
// File: test.js
68-
import test from 'ava';
69-
70-
test('foo', t => {
71-
t.pass();
72-
});
73-
74-
// File: lib/foo.test.js
75-
// with { "files": ["lib/**/*.test.js", "utils/**/*.test.js"] }
76-
// in either `package.json` under 'ava key' or in the rule options
77-
import test from 'ava';
78-
79-
test('foo', t => {
80-
t.pass();
81-
});
82-
```
83-
84-
## Options
85-
86-
This rule supports the following options:
87-
88-
`files`: An array of strings representing the files glob that AVA will use to find test files. Overrides the default and the configuration found in the `package.json` or `ava.config.js` files.
89-
90-
You can set the options like this:
91-
92-
```js
93-
"ava/no-ignored-test-files": ["error", {"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}]
9438
```

docs/rules/no-import-test-files.md

-71
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/re
44

55
This rule will verify that you don't import any test files. It will consider the root of the project to be the closest folder containing a `package.json` file, and will not do anything if it can't find one. Test files in `node_modules` will not be linted as they are ignored by ESLint.
66

7-
Note that this rule will not be able to warn correctly if you use AVA by specifying the files in the command line ( `ava "lib/**/*.test.js"` ). Prefer configuring AVA as described [here](https://github.com/avajs/ava/blob/master/docs/06-configuration.md).
8-
97

108
## Fail
119

@@ -25,30 +23,6 @@ test('foo', t => {
2523
});
2624
```
2725

28-
```js
29-
// File: utils/index.js
30-
// with `{"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}`
31-
// in either `package.json` under the `"ava"` key or in the rule options
32-
// Invalid because the imported file matches `lib/**/*.test.js`
33-
import tests from '../lib/index.test.js';
34-
35-
test('foo', t => {
36-
t.pass();
37-
});
38-
```
39-
40-
```js
41-
// File: utils/index.js
42-
// with `{"extensions": ["js", "mjs"]}`
43-
// in either `package.json` under the `"ava"` key or in the rule options
44-
// Invalid because the imported file extension matches `mjs`
45-
import tests from 'index.test.mjs';
46-
47-
test('foo', t => {
48-
t.pass();
49-
});
50-
```
51-
5226

5327
## Pass
5428

@@ -62,48 +36,3 @@ import sinon from 'sinon';
6236
// File: src/index.js
6337
import utils from './utils';
6438
```
65-
66-
```js
67-
// File: lib/index.js
68-
// with `{"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}`
69-
// in either `package.json` under 'ava key' or in the rule options
70-
import utils from 'test.js';
71-
```
72-
73-
```js
74-
// File: lib/index.js
75-
// `with {"extensions": ["js", "mjs"]}`
76-
// in either `package.json` under 'ava key' or in the rule options
77-
import utils from 'test.jsx';
78-
```
79-
80-
81-
## Options
82-
83-
This rule supports the following options:
84-
85-
`files`: An array of strings representing the files glob that AVA will use to find test files. Overrides the default and the configuration found in the `package.json` or `ava.config.js` files.
86-
87-
You can set the options like this:
88-
89-
```json
90-
"ava/no-ignored-test-files": ["error", {"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}]
91-
```
92-
93-
`extensions`: An array of strings representing the file extensions that AVA will use to find the test files. It overrides the default and the configuration found in the `package.json` or `ava.config.js` files.
94-
95-
This extension will filter out files from the `files` option.
96-
97-
You can set the options like this:
98-
99-
```json
100-
"ava/no-ignored-test-files": [
101-
"error",
102-
{
103-
"extensions": [
104-
"js",
105-
"mjs"
106-
]
107-
}
108-
]
109-
```

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
"mocha"
3030
],
3131
"dependencies": {
32-
"arrify": "^2.0.1",
3332
"deep-strict-equal": "^0.2.0",
3433
"enhance-visitors": "^1.0.0",
35-
"esm": "^3.2.25",
3634
"espree": "^5.0.0",
3735
"espurify": "^2.0.0",
3836
"import-modules": "^1.1.0",
39-
"is-plain-object": "^3.0.0",
40-
"multimatch": "^4.0.0",
41-
"pkg-up": "^3.1.0"
37+
"pkg-dir": "^4.2.0",
38+
"resolve-from": "^5.0.0"
4239
},
4340
"devDependencies": {
4441
"ava": "^1.0.1",

rules/no-ignored-test-files.js

+15-55
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,18 @@
11
'use strict';
2-
const path = require('path');
3-
const arrify = require('arrify');
4-
const pkgUp = require('pkg-up');
5-
const multimatch = require('multimatch');
62
const {visitIf} = require('enhance-visitors');
73
const util = require('../util');
84
const createAvaRule = require('../create-ava-rule');
95

10-
const excludedFolders = [
11-
'**/fixtures/**',
12-
'**/helpers/**'
13-
];
14-
15-
function isIgnored(rootDir, files, filepath) {
16-
const relativeFilePath = path.relative(rootDir, filepath);
17-
18-
if (multimatch([relativeFilePath], excludedFolders).length !== 0) {
19-
return `Test file is ignored because it is in \`${excludedFolders.join(' ')}\`.`;
20-
}
21-
22-
if (multimatch([relativeFilePath], files).length === 0) {
23-
return `Test file is ignored because it is not in \`${files.join(' ')}\`.`;
24-
}
25-
}
26-
27-
function getPackageInfo() {
28-
const packageFilePath = pkgUp.sync();
29-
30-
return {
31-
rootDir: packageFilePath && path.dirname(packageFilePath),
32-
files: util.getAvaConfig(packageFilePath).files
33-
};
34-
}
35-
366
const create = context => {
377
const filename = context.getFilename();
388

399
if (filename === '<text>') {
4010
return {};
4111
}
4212

43-
const ava = createAvaRule();
44-
const packageInfo = getPackageInfo();
45-
const options = context.options[0] || {};
46-
const files = arrify(options.files || packageInfo.files || util.defaultFiles);
4713
let hasTestCall = false;
4814

49-
if (!packageInfo.rootDir) {
50-
// Could not find a package.json folder
51-
return {};
52-
}
53-
15+
const ava = createAvaRule();
5416
return ava.merge({
5517
CallExpression: visitIf([
5618
ava.isInTestFile,
@@ -63,36 +25,34 @@ const create = context => {
6325
return;
6426
}
6527

66-
const ignoredReason = isIgnored(packageInfo.rootDir, files, filename);
28+
const avaHelper = util.loadAvaHelper(filename);
29+
if (!avaHelper) {
30+
return {};
31+
}
6732

68-
if (ignoredReason) {
69-
context.report({
70-
node,
71-
message: ignoredReason
72-
});
33+
const {isHelper, isSource, isTest} = avaHelper.classifyFile(filename);
34+
35+
if (!isTest) {
36+
if (isHelper) {
37+
context.report({node, message: 'AVA treats this as a helper file.'});
38+
} else if (isSource) {
39+
context.report({node, message: 'AVA treats this as a source file.'});
40+
} else {
41+
context.report({node, message: 'AVA ignores this file.'});
42+
}
7343
}
7444

7545
hasTestCall = false;
7646
}
7747
});
7848
};
7949

80-
const schema = [{
81-
type: 'object',
82-
properties: {
83-
files: {
84-
type: 'array'
85-
}
86-
}
87-
}];
88-
8950
module.exports = {
9051
create,
9152
meta: {
9253
docs: {
9354
url: util.getDocsUrl(__filename)
9455
},
95-
schema,
9656
type: 'suggestion'
9757
}
9858
};

0 commit comments

Comments
 (0)