Skip to content

Commit 3647321

Browse files
committed
Refactor glob handling
* Require globs to match files, not directories * files and sources must be an array of one or more patterns, not a string * Only accept files via the CLI, not globs or directories * Upgrade globby * At this stage, only test files starting with _ are treated as helpers. Helper globs should be made configurable. * It's no longer possible to override an exclusion pattern. We may reintroduce this at a later stage
1 parent d30d437 commit 3647321

File tree

54 files changed

+882
-926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+882
-926
lines changed

docs/05-command-line.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
66
$ npx ava --help
77

88
Usage
9-
ava [<file|directory|glob> ...]
9+
ava [<file> ...]
1010

1111
Options
1212
--watch, -w Re-run tests when tests and source files change
@@ -28,13 +28,22 @@ $ npx ava --help
2828
ava test-*.js
2929
ava test
3030

31-
Default patterns when no arguments:
32-
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js
31+
The above relies on your shell expanding the glob patterns.
32+
Without arguments, AVA uses the following patterns:
33+
**/test.js **/test-*.js **/*.test.js **/test/**/*.js **/__tests__/**/*.js
3334
```
3435

3536
*Note that the CLI will use your local install of AVA when available, even when run globally.*
3637

37-
Directories are recursed, with all `*.js` files being treated as test files. Directories named `fixtures`, `helpers` and `node_modules` are *always* ignored. So are files starting with `_` which allows you to place helpers in the same directory as your test files.
38+
AVA searches for test files using the following patterns:
39+
40+
* `**/test.js`
41+
* `**/test-*.js`
42+
* `**/*.test.js`
43+
* `**/test/**/*.js`
44+
* `**/__tests__/**/*.js`
45+
46+
Files inside `node_modules` are *always* ignored. So are files starting with `_`. These are treated as helpers.
3847

3948
When using `npm test`, you can pass positional arguments directly `npm test test2.js`, but flags needs to be passed like `npm test -- --verbose`.
4049

docs/06-configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ To ignore a file or directory, prefix the pattern with an `!` (exclamation mark)
1313
"ava": {
1414
"files": [
1515
"my-test-directory/**/*.js",
16-
"!my-test-directory/exclude-this-directory/**/*.js",
16+
"!my-test-directory/exclude-this-directory",
1717
"!**/exclude-this-file.js"
1818
],
1919
"sources": [
2020
"**/*.{js,jsx}",
21-
"!dist/**/*"
21+
"!dist"
2222
],
2323
"match": [
2424
"*oo",
@@ -48,7 +48,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
4848

4949
## Options
5050

51-
- `files`: file & directory paths and glob patterns that select which files AVA will run tests from. Files with an underscore prefix are ignored. All matched files in selected directories are run. By default only selects files with `js` extensions, even if the glob pattern matches other files. Specify `extensions` and `babel.extensions` to allow other file extensions
51+
- `files`: glob patterns that select which files AVA will run tests from. Files with an underscore prefix are ignored. By default only selects files with `js` extensions, even if the glob pattern matches other files. Specify `extensions` and `babel.extensions` to allow other file extensions
5252
- `sources`: files that, when changed, cause tests to be re-run in watch mode. See the [watch mode recipe for details](https://github.com/avajs/ava/blob/master/docs/recipes/watch-mode.md#source-files-and-test-files)
5353
- `match`: not typically useful in the `package.json` configuration, but equivalent to [specifying `--match` on the CLI](./05-command-line.md#running-tests-with-matching-titles)
5454
- `cache`: cache compiled test and helper files under `node_modules/.cache/ava`. If `false`, files are cached in a temporary directory instead
@@ -64,7 +64,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
6464
- `babel.extensions`: extensions of test files that will be precompiled using AVA's Babel presets. Setting this overrides the default `"js"` value, so make sure to include that extension in the list
6565
- `timeout`: Timeouts in AVA behave differently than in other test frameworks. AVA resets a timer after each test, forcing tests to quit if no new test results were received within the specified timeout. This can be used to handle stalled tests. See our [timeout documentation](./07-test-timeouts.md) for more options.
6666

67-
Note that providing files on the CLI overrides the `files` option. If you've configured a glob pattern, for instance `test/**/*.test.js`, you may want to repeat it when using the CLI: `ava 'test/integration/*.test.js'`.
67+
Note that providing files on the CLI overrides the `files` option.
6868

6969
## Using `ava.config.js`
7070

docs/08-common-pitfalls.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ test('one is one', t => {
8383
});
8484
```
8585

86-
### Helpers are not compiled when using a non-default test folder
87-
88-
This is a [known issue](https://github.com/avajs/ava/issues/1319). You should put your tests in a folder called `test` or `__tests__`.
89-
9086
---
9187

9288
Is your problem not listed here? Submit a pull request or comment on [this issue](https://github.com/avajs/ava/issues/404).

docs/recipes/watch-mode.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ By default AVA watches for changes to the test files, snapshot files, `package.j
7171

7272
You can configure patterns for the source files in the [`ava` section of your `package.json`, or `ava.config.js` file][config], using the `sources` key.
7373

74-
You can specify patterns to match files in the folders that would otherwise be ignored, e.g. use `node_modules/some-dependency/*.js` to specify all `.js` files in `node_modules/some-dependency` as a source, even though normally all files in `node_modules` are ignored. Note that you need to specify an exact directory; `{bower_components,node_modules}/**/*.js` won't work.
75-
7674
If your tests write to disk they may trigger the watcher to rerun your tests. Configure patterns for the source files to avoid this.
7775

7876
## Dependency tracking

lib/api.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const ms = require('ms');
1616
const chunkd = require('chunkd');
1717
const Emittery = require('emittery');
1818
const babelPipeline = require('./babel-pipeline');
19+
const globs = require('./globs');
1920
const RunStatus = require('./run-status');
20-
const AvaFiles = require('./ava-files');
2121
const fork = require('./fork');
2222
const serializeError = require('./serialize-error');
2323

@@ -50,7 +50,9 @@ class Api extends Emittery {
5050
}
5151
}
5252

53-
async run(files, runtimeOptions = {}) {
53+
async run(files = [], runtimeOptions = {}) {
54+
files = files.map(file => path.resolve(this.options.resolveTestsFrom, file));
55+
5456
const apiOptions = this.options;
5557

5658
// Each run will have its own status. It can only be created when test files
@@ -105,8 +107,17 @@ class Api extends Emittery {
105107
};
106108

107109
try {
108-
// Find all test files.
109-
files = await new AvaFiles({cwd: apiOptions.resolveTestsFrom, files, extensions: this._allExtensions}).findTestFiles();
110+
const precompiler = await this._setupPrecompiler();
111+
let helpers = [];
112+
if (files.length === 0 || precompiler.enabled) {
113+
const found = await globs.findHelpersAndTests({cwd: this.options.resolveTestsFrom, ...apiOptions.globs});
114+
if (files.length === 0) {
115+
({tests: files} = found);
116+
}
117+
118+
({helpers} = found);
119+
}
120+
110121
if (this.options.parallelRuns) {
111122
const {currentIndex, totalRuns} = this.options.parallelRuns;
112123
const fileCount = files.length;
@@ -157,22 +168,21 @@ class Api extends Emittery {
157168
}
158169
});
159170

160-
let precompilation = await this._setupPrecompiler();
161-
if (precompilation.enabled) {
171+
let precompilation = null;
172+
if (precompiler.enabled) {
162173
// Compile all test and helper files. Assumes the tests only load
163174
// helpers from within the `resolveTestsFrom` directory. Without
164175
// arguments this is the `projectDir`, else it's `process.cwd()`
165176
// which may be nested too deeply.
166-
const helpers = await new AvaFiles({cwd: this.options.resolveTestsFrom, extensions: this._allExtensions}).findTestHelpers();
167177
precompilation = {
168-
cacheDir: precompilation.cacheDir,
178+
cacheDir: precompiler.cacheDir,
169179
map: [...files, ...helpers].reduce((acc, file) => {
170180
try {
171181
const realpath = fs.realpathSync(file);
172182
const filename = path.basename(realpath);
173183
const cachePath = this._regexpFullExtensions.test(filename) ?
174-
precompilation.precompileFull(realpath) :
175-
precompilation.precompileEnhancementsOnly(realpath);
184+
precompiler.precompileFull(realpath) :
185+
precompiler.precompileEnhancementsOnly(realpath);
176186
if (cachePath) {
177187
acc[realpath] = cachePath;
178188
}
@@ -183,8 +193,6 @@ class Api extends Emittery {
183193
return acc;
184194
}, {})
185195
};
186-
} else {
187-
precompilation = null;
188196
}
189197

190198
// Resolve the correct concurrency value.

0 commit comments

Comments
 (0)