Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
95 changes: 15 additions & 80 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con

## Options

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ت


- `files`: an array of glob patterns to select test files. Files with an underscore prefix are ignored. By default only selects files with `cjs`, `mjs` & `js` extensions, even if the pattern matches other files. Specify `extensions` to allow other file extensions
- `files`: an array of glob patterns to select test files. Files with an underscore prefix are ignored. By default only selects files with `mjs` & `js` extensions, even if the pattern matches other files. Specify `extensions` to allow other file extensions
- `watchMode`: See the [watch mode recipe for details](https://github.com/avajs/ava/blob/main/docs/recipes/watch-mode.md)
- `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`: defaults to `true` to cache compiled files under `node_modules/.cache/ava`. If `false`, files are cached in a temporary directory instead
Expand All @@ -55,7 +55,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
- `tap`: if `true`, enables the [TAP reporter](./05-command-line.md#tap-reporter)
- `verbose`: if `true`, enables verbose output (though there currently non-verbose output is not supported)
- `snapshotDir`: specifies a fixed location for storing snapshot files. Use this if your snapshots are ending up in the wrong location
- `extensions`: extensions of test files. Setting this overrides the default `["cjs", "mjs", "js"]` value, so make sure to include those extensions in the list. [Experimentally you can configure how files are loaded](#configuring-module-formats)
- `extensions`: extensions of test files. Setting this overrides the default `["mjs", "js"]` value, so make sure to include those extensions in the list.
- `require`: [extra modules to load before test files](#requiring-extra-modules)
- `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.
- `nodeArguments`: Configure Node.js arguments used to launch worker processes.
Expand All @@ -68,18 +68,18 @@ Provide the `typescript` option (and install [`@ava/typescript`](https://github.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ر

## Using `ava.config.*` files

Rather than specifying the configuration in the `package.json` file you can use `ava.config.js`, `ava.config.cjs` or `ava.config.mjs` files.
Rather than specifying the configuration in the `package.json` file you can use an `ava.config.js` file. If your project does not already treat `.js` files as ES modules, use `ava.config.mjs` instead.

To use these files:

1. Your `package.json` must not contain an `ava` property (or, if it does, it must be an empty object)
2. You must only have one `ava.config.*` file in any directory, so don't mix `ava.config.js` *and* `ava.config.cjs` files
2. You must only have one `ava.config.*` file in any directory, so don't mix `ava.config.js` *and* `ava.config.mjs` files

AVA searches your file system for `ava.config.*` files. First, when you run AVA, it finds the closest `package.json`. Starting in that directory it recursively checks the parent directories until it either reaches the file system root or encounters a `.git` file or directory. The first `ava.config.*` file found is selected. This allows you to use a single configuration file in a monorepo setup.

### `ava.config.js`

AVA follows Node.js' behavior, so if you've set `"type": "module"` you must use ESM, and otherwise you must use CommonJS.
Prefer `ava.config.js` when your project already treats `.js` files as ES modules, for example through `"type": "module"` in `package.json`.

The default export can either be a plain object or a factory function which returns a plain object. You can export or return a promise for a plain object:

Expand Down Expand Up @@ -113,44 +113,10 @@ export default ({projectDir}) => {
};
```

### `ava.config.cjs`

For `ava.config.cjs` files you must assign `module.exports`. ["Module scope"](https://nodejs.org/docs/latest-v12.x/api/modules.html#modules_the_module_scope) is available. You can `require()` dependencies.

The module export can either be a plain object or a factory function which returns a plain object. You can export or return a promise for a plain object:

```js
module.exports = {
require: ['./_my-test-helper.js']
};
```

```js
module.exports = () => {
return {
require: ['./_my-test-helper.js']
};
};
```

The factory function is called with an object containing a `projectDir` property, which you could use to change the returned configuration:

```js
module.exports = ({projectDir}) => {
if (projectDir === '/Users/username/projects/my-project') {
return {
// Config A
};
}

return {
// Config B
};
};
```

### `ava.config.mjs`

Use `ava.config.mjs` when you need the filename itself to opt into ESM.

The default export can either be a plain object or a factory function which returns a plain object. You can export or return a promise for a plain object:

```js
Expand Down Expand Up @@ -185,32 +151,32 @@ export default ({projectDir}) => {

## Alternative configuration files

The [CLI] lets you specify a specific configuration file, using the `--config` flag. This file must have either a `.js`, `.cjs` or `.mjs` extension and is processed like an `ava.config.js`, `ava.config.cjs` or `ava.config.mjs` file would be.
The [CLI] lets you specify a specific configuration file, using the `--config` flag. Prefer a `.js` file. Use `.mjs` only when your project does not already treat `.js` files as ESM.

When the `--config` flag is set, the provided file will override all configuration from the `package.json` and `ava.config.js`, `ava.config.cjs` or `ava.config.mjs` files. The configuration is not merged.
When the `--config` flag is set, the provided file will override all configuration from the `package.json` and `ava.config.js` or `ava.config.mjs` files. The configuration is not merged.

You can use this to customize configuration for a specific test run. For instance, you may want to run unit tests separately from integration tests:

`ava.config.cjs`:
`ava.config.js`:

```js
module.exports = {
export default {
files: ['unit-tests/**/*']
};
```

`integration-tests.config.cjs`:
`integration-tests.config.js`:

```js
const baseConfig = require('./ava.config.cjs');
import baseConfig from './ava.config.js';

module.exports = {
export default {
...baseConfig,
files: ['integration-tests/**/*']
};
```

You can now run your unit tests through `npx ava` and the integration tests through `npx ava --config integration-tests.config.cjs`.
You can now run your unit tests through `npx ava` and the integration tests through `npx ava --config integration-tests.config.js`.

## Object printing depth

Expand Down Expand Up @@ -243,22 +209,6 @@ export default {
};
```

## Configuring module formats

Node.js can only load non-standard extension as ES Modules when using [experimental loaders](https://nodejs.org/docs/latest/api/esm.html#esm_experimental_loaders). To use this you'll also have to configure AVA to `import()` your test file.

As with the array form, you need to explicitly list `js`, `cjs`, and `mjs` extensions. These **must** be set using the `true` value; other extensions are configurable using either `'commonjs'` or `'module'`:

`ava.config.js`:
```js
export default {
extensions: {
js: true,
ts: 'module'
}
};
```

## Requiring extra modules

Use the `require` configuration to load extra modules before test files are loaded. Relative paths are resolved against the project directory and can be loaded through `@ava/typescript`. Otherwise, modules are loaded from within the `node_modules` directory inside the project.
Expand Down Expand Up @@ -286,21 +236,6 @@ export default function () {
}
```

`_my-test-helper.cjs`:
```js
module.exports = function () {
// Additional setup
}
```

In CJS files, a `default` export is also supported:

```js
exports.default = function () {
// Never called
}
```

You can provide arguments:

`ava.config.js`:
Expand Down
4 changes: 2 additions & 2 deletions docs/08-common-pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ It is best practice to make sure your code exits cleanly. We've also seen occurr

For these reasons we're not providing an option to disable this timeout behavior. However, it is possible to register a callback for when AVA has completed the test run without uncaught exceptions or unhandled rejections. From inside this callback you can do whatever you need to do, including calling `process.exit()`.

Create a `_force-exit.mjs` file:
Create a `_force-exit.js` file:

```js
import process from 'node:process';
Expand All @@ -108,7 +108,7 @@ Load it for all test files through AVA's `require` option:

```js
export default {
require: ['./_force-exit.mjs'],
require: ['./_force-exit.js'],
};
```

Expand Down
18 changes: 2 additions & 16 deletions docs/recipes/babel.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/docs/recipes/babel.md)

You can enable Babel support by installing [`@babel/register`](https://babeljs.io/docs/en/babel-register) and `@babel/core`, and then in AVA's configuration requiring `@babel/register`:
AVA is ESM-only. [`@babel/register`](https://babeljs.io/docs/en/babel-register) only hooks legacy `require()` loading, so this setup is no longer supported.

**`package.json`:**

```json
{
"ava": {
"require": [
"@babel/register"
]
}
}
```

`@babel/register` is compatible with CommonJS only. It intercepts `require()` calls and compiles files on the fly. This will compile source, helper and test files.

For more information visit the [Babel documentation](https://babeljs.io/docs/en/babel-register).
If you need Babel, precompile your files before running AVA or use an ESM-capable loader configured through [`nodeArguments`](../06-configuration.md#node-arguments).
6 changes: 3 additions & 3 deletions docs/recipes/debugging-with-vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Alternatively you can create a launch configuration, which makes it easier to de
"type": "node",
"request": "launch",
"name": "Debug AVA test file",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.mjs",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.js",
"args": [
"${file}"
],
Expand Down Expand Up @@ -56,7 +56,7 @@ Assuming the names of your test files are unique you could try the following con
"type": "node",
"request": "launch",
"name": "Debug AVA test file",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.mjs",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.js",
"args": [
"build/**/${fileBasenameNoExtension}.*"
],
Expand Down Expand Up @@ -103,7 +103,7 @@ Or, if you're using a launch configuration, add the `--serial` argument:
"type": "node",
"request": "launch",
"name": "Debug AVA test file",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.mjs",
"program": "${workspaceFolder}/node_modules/ava/entrypoints/cli.js",
"args": [
"--serial",
"${file}"
Expand Down
6 changes: 3 additions & 3 deletions docs/recipes/puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The first step is setting up a helper to configure the environment:
```js
import puppeteer from 'puppeteer';

module.exports = async (t, run) => {
export default async (t, run) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
Expand All @@ -24,7 +24,7 @@ module.exports = async (t, run) => {
await page.close();
await browser.close();
}
}
};
```

## Usage example
Expand All @@ -33,7 +33,7 @@ module.exports = async (t, run) => {

```js
import test from 'ava';
import withPage from './_withPage';
import withPage from './_withPage.js';

const url = 'https://google.com';

Expand Down
22 changes: 2 additions & 20 deletions docs/recipes/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ There are two components to a setup like this:

```json
"ava": {
"extensions": {
"ts": "module"
},
"extensions": ["ts"],
"nodeArguments": [
"--import=tsimp"
]
Expand All @@ -50,7 +48,7 @@ There are two components to a setup like this:

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)

Create a `test.ts` file. ESM syntax works best, even if you're targeting CommonJS.
Create a `test.ts` file using ESM syntax.

```ts
import test from 'ava';
Expand All @@ -62,22 +60,6 @@ test('fn() returns foo', t => {
});
```

You can use CommonJS syntax as well:

```ts
const test = require('ava');
```

This works whether `esModuleInterop` is enabled or not.

`import … = require()` syntax is less elegant. It's best like this:

```ts
import ava = require('ava')

const test = ava.default;
```

## Using [macros](../01-writing-tests.md#reusing-test-logic-through-macros)

Macros can receive additional arguments. AVA can infer these to ensure you're using the macro correctly:
Expand Down
57 changes: 3 additions & 54 deletions docs/recipes/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,11 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/docs
- Optional: [babel-plugin-webpack-alias-7](https://github.com/shortminds/babel-plugin-webpack-alias-7) if you want to use [webpack aliases](https://webpack.js.org/configuration/resolve/#resolve-alias) or use them in your source files
- `npm i --save-dev babel-plugin-webpack-alias-7`

## Setup
## Status

The first step is setting up a helper to configure the environment to transpile `.vue` files and run in a browser like environment.
This recipe relied on legacy `require()` hooks. AVA is ESM-only, so this setup is no longer supported.

**`package.json`:**

```json
{
"ava": {
"require": [
"./test/_setup.js"
]
}
}
```

```js
// ./test/_setup.cjs

// Set up JSDom.
const jsdomGlobal = require('jsdom-global');
jsdomGlobal();

// Fix the Date object, see <https://github.com/vuejs/vue-test-utils/issues/936#issuecomment-415386167>.
window.Date = Date

// Setup browser environment
const hooks = require('require-extension-hooks');
const Vue = require('vue');

// Setup Vue.js to remove production tip
Vue.config.productionTip = false;

// Setup vue files to be processed by `require-extension-hooks-vue`
hooks('vue').plugin('vue').push();
// Setup vue and js files to be processed by `require-extension-hooks-babel`
hooks(['vue', 'js']).exclude(({filename}) => filename.match(/\/node_modules\//)).plugin('babel').push();
```

**Note:** If you are using _babel-plugin-webpack-alias-7_, you must also exclude your webpack file - e.g. `filename.includes(/\/node_modules\//) || filename.includes('webpack.config.test.js')`

## Sample snapshot test

```js
const test = require('ava');
const Vue = require('vue');
const Component = require('component.vue');

test('renders', t => {
const vm = new Vue(Component).$mount();
const tree = {
$el: vm.$el.outerHTML
};
t.snapshot(tree);
});
```
Precompile your Vue components before running AVA, or use tooling that provides an ESM loader for `.vue` files.

## Coverage reporting

Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/watch-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Afterwards you can use the `r` command to run the matched tests again, or `a` co

AVA tracks which source files your test files depend on. If you change such a dependency only the test file that depends on it will be rerun. AVA will rerun all tests if it cannot determine which test file depends on the changed source file.

Dependency tracking works for `require()` and `import` syntax, as supported by [@vercel/nft](https://github.com/vercel/nft). `import()` is supported but dynamic paths such as `import(myVariable)` are not.
Dependency tracking works for static `import` syntax, as supported by [@vercel/nft](https://github.com/vercel/nft). `import()` is supported but dynamic paths such as `import(myVariable)` are not.

Files accessed using the `fs` module are not tracked.

Expand Down
File renamed without changes.
Loading
Loading