Skip to content

Commit

Permalink
Refactor glob handling
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
novemberborn committed Apr 28, 2019
1 parent d30d437 commit 3647321
Show file tree
Hide file tree
Showing 54 changed files with 882 additions and 926 deletions.
17 changes: 13 additions & 4 deletions docs/05-command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
$ npx ava --help

Usage
ava [<file|directory|glob> ...]
ava [<file> ...]

Options
--watch, -w Re-run tests when tests and source files change
Expand All @@ -28,13 +28,22 @@ $ npx ava --help
ava test-*.js
ava test

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

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

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.
AVA searches for test files using the following patterns:

* `**/test.js`
* `**/test-*.js`
* `**/*.test.js`
* `**/test/**/*.js`
* `**/__tests__/**/*.js`

Files inside `node_modules` are *always* ignored. So are files starting with `_`. These are treated as helpers.

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

Expand Down
8 changes: 4 additions & 4 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ To ignore a file or directory, prefix the pattern with an `!` (exclamation mark)
"ava": {
"files": [
"my-test-directory/**/*.js",
"!my-test-directory/exclude-this-directory/**/*.js",
"!my-test-directory/exclude-this-directory",
"!**/exclude-this-file.js"
],
"sources": [
"**/*.{js,jsx}",
"!dist/**/*"
"!dist"
],
"match": [
"*oo",
Expand Down Expand Up @@ -48,7 +48,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con

## Options

- `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
- `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
- `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)
- `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)
- `cache`: cache compiled test and helper files under `node_modules/.cache/ava`. If `false`, files are cached in a temporary directory instead
Expand All @@ -64,7 +64,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
- `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
- `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.

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'`.
Note that providing files on the CLI overrides the `files` option.

## Using `ava.config.js`

Expand Down
4 changes: 0 additions & 4 deletions docs/08-common-pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ test('one is one', t => {
});
```

### Helpers are not compiled when using a non-default test folder

This is a [known issue](https://github.com/avajs/ava/issues/1319). You should put your tests in a folder called `test` or `__tests__`.

---

Is your problem not listed here? Submit a pull request or comment on [this issue](https://github.com/avajs/ava/issues/404).
2 changes: 0 additions & 2 deletions docs/recipes/watch-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ By default AVA watches for changes to the test files, snapshot files, `package.j

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.

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.

If your tests write to disk they may trigger the watcher to rerun your tests. Configure patterns for the source files to avoid this.

## Dependency tracking
Expand Down
32 changes: 20 additions & 12 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const ms = require('ms');
const chunkd = require('chunkd');
const Emittery = require('emittery');
const babelPipeline = require('./babel-pipeline');
const globs = require('./globs');
const RunStatus = require('./run-status');
const AvaFiles = require('./ava-files');
const fork = require('./fork');
const serializeError = require('./serialize-error');

Expand Down Expand Up @@ -50,7 +50,9 @@ class Api extends Emittery {
}
}

async run(files, runtimeOptions = {}) {
async run(files = [], runtimeOptions = {}) {
files = files.map(file => path.resolve(this.options.resolveTestsFrom, file));

const apiOptions = this.options;

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

try {
// Find all test files.
files = await new AvaFiles({cwd: apiOptions.resolveTestsFrom, files, extensions: this._allExtensions}).findTestFiles();
const precompiler = await this._setupPrecompiler();
let helpers = [];
if (files.length === 0 || precompiler.enabled) {
const found = await globs.findHelpersAndTests({cwd: this.options.resolveTestsFrom, ...apiOptions.globs});
if (files.length === 0) {
({tests: files} = found);
}

({helpers} = found);
}

if (this.options.parallelRuns) {
const {currentIndex, totalRuns} = this.options.parallelRuns;
const fileCount = files.length;
Expand Down Expand Up @@ -157,22 +168,21 @@ class Api extends Emittery {
}
});

let precompilation = await this._setupPrecompiler();
if (precompilation.enabled) {
let precompilation = null;
if (precompiler.enabled) {
// Compile all test and helper files. Assumes the tests only load
// helpers from within the `resolveTestsFrom` directory. Without
// arguments this is the `projectDir`, else it's `process.cwd()`
// which may be nested too deeply.
const helpers = await new AvaFiles({cwd: this.options.resolveTestsFrom, extensions: this._allExtensions}).findTestHelpers();
precompilation = {
cacheDir: precompilation.cacheDir,
cacheDir: precompiler.cacheDir,
map: [...files, ...helpers].reduce((acc, file) => {
try {
const realpath = fs.realpathSync(file);
const filename = path.basename(realpath);
const cachePath = this._regexpFullExtensions.test(filename) ?
precompilation.precompileFull(realpath) :
precompilation.precompileEnhancementsOnly(realpath);
precompiler.precompileFull(realpath) :
precompiler.precompileEnhancementsOnly(realpath);
if (cachePath) {
acc[realpath] = cachePath;
}
Expand All @@ -183,8 +193,6 @@ class Api extends Emittery {
return acc;
}, {})
};
} else {
precompilation = null;
}

// Resolve the correct concurrency value.
Expand Down
Loading

0 comments on commit 3647321

Please sign in to comment.