Skip to content

Commit

Permalink
Rely on AVA to provide a helper for our rules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
novemberborn authored May 28, 2019
1 parent a184661 commit df6374a
Show file tree
Hide file tree
Showing 30 changed files with 131 additions and 621 deletions.
68 changes: 6 additions & 62 deletions docs/rules/no-ignored-test-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,22 @@

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

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).
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.

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.

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.

## Fail

```js
// File: test/foo/fixtures/bar.js
// Invalid because in `fixtures` folder
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: test/foo/helpers/bar.js
// Invalid because in `helpers` folder
// File: test/_helper.js
// Invalid because a helper.
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: lib/foo.test.js
// Invalid because not in the searched files
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: test.js
// with { "files": ["lib/**/*.test.js", "utils/**/*.test.js"] }
// in either `package.json` under 'ava key' or in the rule options
// Invalid because not in the searched files
// File: lib/foo.js
// Invalid because not a test file.
import test from 'ava';

test('foo', t => {
Expand All @@ -50,45 +29,10 @@ test('foo', t => {
## Pass

```js
// File: test/foo/not-fixtures/bar.js
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: test/foo/not-helpers/bar.js
// File: test/foo.js
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: test.js
import test from 'ava';

test('foo', t => {
t.pass();
});

// File: lib/foo.test.js
// with { "files": ["lib/**/*.test.js", "utils/**/*.test.js"] }
// in either `package.json` under 'ava key' or in the rule options
import test from 'ava';

test('foo', t => {
t.pass();
});
```

## Options

This rule supports the following options:

`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.

You can set the options like this:

```js
"ava/no-ignored-test-files": ["error", {"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}]
```
71 changes: 0 additions & 71 deletions docs/rules/no-import-test-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/re

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.

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).


## Fail

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

```js
// File: utils/index.js
// with `{"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}`
// in either `package.json` under the `"ava"` key or in the rule options
// Invalid because the imported file matches `lib/**/*.test.js`
import tests from '../lib/index.test.js';

test('foo', t => {
t.pass();
});
```

```js
// File: utils/index.js
// with `{"extensions": ["js", "mjs"]}`
// in either `package.json` under the `"ava"` key or in the rule options
// Invalid because the imported file extension matches `mjs`
import tests from 'index.test.mjs';

test('foo', t => {
t.pass();
});
```


## Pass

Expand All @@ -62,48 +36,3 @@ import sinon from 'sinon';
// File: src/index.js
import utils from './utils';
```

```js
// File: lib/index.js
// with `{"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}`
// in either `package.json` under 'ava key' or in the rule options
import utils from 'test.js';
```

```js
// File: lib/index.js
// `with {"extensions": ["js", "mjs"]}`
// in either `package.json` under 'ava key' or in the rule options
import utils from 'test.jsx';
```


## Options

This rule supports the following options:

`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.

You can set the options like this:

```json
"ava/no-ignored-test-files": ["error", {"files": ["lib/**/*.test.js", "utils/**/*.test.js"]}]
```

`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.

This extension will filter out files from the `files` option.

You can set the options like this:

```json
"ava/no-ignored-test-files": [
"error",
{
"extensions": [
"js",
"mjs"
]
}
]
```
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@
"mocha"
],
"dependencies": {
"arrify": "^2.0.1",
"deep-strict-equal": "^0.2.0",
"enhance-visitors": "^1.0.0",
"esm": "^3.2.25",
"espree": "^5.0.0",
"espurify": "^2.0.0",
"import-modules": "^1.1.0",
"is-plain-object": "^3.0.0",
"multimatch": "^4.0.0",
"pkg-up": "^3.1.0"
"pkg-dir": "^4.2.0",
"resolve-from": "^5.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
Expand Down
70 changes: 15 additions & 55 deletions rules/no-ignored-test-files.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,18 @@
'use strict';
const path = require('path');
const arrify = require('arrify');
const pkgUp = require('pkg-up');
const multimatch = require('multimatch');
const {visitIf} = require('enhance-visitors');
const util = require('../util');
const createAvaRule = require('../create-ava-rule');

const excludedFolders = [
'**/fixtures/**',
'**/helpers/**'
];

function isIgnored(rootDir, files, filepath) {
const relativeFilePath = path.relative(rootDir, filepath);

if (multimatch([relativeFilePath], excludedFolders).length !== 0) {
return `Test file is ignored because it is in \`${excludedFolders.join(' ')}\`.`;
}

if (multimatch([relativeFilePath], files).length === 0) {
return `Test file is ignored because it is not in \`${files.join(' ')}\`.`;
}
}

function getPackageInfo() {
const packageFilePath = pkgUp.sync();

return {
rootDir: packageFilePath && path.dirname(packageFilePath),
files: util.getAvaConfig(packageFilePath).files
};
}

const create = context => {
const filename = context.getFilename();

if (filename === '<text>') {
return {};
}

const ava = createAvaRule();
const packageInfo = getPackageInfo();
const options = context.options[0] || {};
const files = arrify(options.files || packageInfo.files || util.defaultFiles);
let hasTestCall = false;

if (!packageInfo.rootDir) {
// Could not find a package.json folder
return {};
}

const ava = createAvaRule();
return ava.merge({
CallExpression: visitIf([
ava.isInTestFile,
Expand All @@ -63,36 +25,34 @@ const create = context => {
return;
}

const ignoredReason = isIgnored(packageInfo.rootDir, files, filename);
const avaHelper = util.loadAvaHelper(filename);
if (!avaHelper) {
return {};
}

if (ignoredReason) {
context.report({
node,
message: ignoredReason
});
const {isHelper, isSource, isTest} = avaHelper.classifyFile(filename);

if (!isTest) {
if (isHelper) {
context.report({node, message: 'AVA treats this as a helper file.'});
} else if (isSource) {
context.report({node, message: 'AVA treats this as a source file.'});
} else {
context.report({node, message: 'AVA ignores this file.'});
}
}

hasTestCall = false;
}
});
};

const schema = [{
type: 'object',
properties: {
files: {
type: 'array'
}
}
}];

module.exports = {
create,
meta: {
docs: {
url: util.getDocsUrl(__filename)
},
schema,
type: 'suggestion'
}
};
Loading

0 comments on commit df6374a

Please sign in to comment.