You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Find ava.config.* files outside of project directory
Fixes#2285.
Find configuration files in parent directories, until a `.git` is
encountered.
Print a warning when AVA is run with a configuration file that is not
next to the `package.json`. This is now allowed when passing `--config`.
Update the documentation to reflect these changes. Remove documentation
of the experimental "next-gen" feature in AVA 3.
Copy file name to clipboardexpand all lines: docs/06-configuration.md
+49-36
Original file line number
Diff line number
Diff line change
@@ -67,23 +67,25 @@ Provide the `typescript` option (and install [`@ava/typescript`](https://github.
67
67
68
68
## Using `ava.config.*` files
69
69
70
-
Rather than specifying the configuration in the `package.json` file you can use `ava.config.js` or `ava.config.cjs` files.
70
+
Rather than specifying the configuration in the `package.json` file you can use `ava.config.js`, `ava.config.cjs` or `ava.config.mjs` files.
71
+
72
+
Note: AVA 3 recognizes `ava.config.mjs` files but refuses to load them. They work in AVA 4.
71
73
72
74
To use these files:
73
75
74
-
1.They must be in the same directory as your `package.json`
75
-
2.Your `package.json`must not contain an`ava` property (or, if it does, it must be an empty object)
76
-
3.You must not both have an `ava.config.js`*and* an `ava.config.cjs` file
76
+
1.Your `package.json`must not contain an `ava` property (or, if it does, it must be an empty object)
77
+
2.You must only have one`ava.config.*` file in any directory, so don't mix `ava.config.js`*and*`ava.config.cjs` files
78
+
3.AVA 3 requires these files be in the same directory as your `package.json` file
77
79
78
-
AVA 3 recognizes `ava.config.mjs` files but refuses to load them. This is changing in AVA 4, [see below](#next-generation-configuration).
80
+
AVA 4 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.
79
81
80
82
### `ava.config.js`
81
83
82
-
In AVA 3, for `ava.config.js` files you must use `export default`. You cannot use ["module scope"](https://nodejs.org/docs/latest-v12.x/api/modules.html#modules_the_module_scope). You cannot import dependencies.
84
+
AVA 4 follows Node.js' behavior, so if you've set `"type": "module"` you must use ESM, and otherwise you must use CommonJS.
83
85
84
-
This is changing in AVA 4, [see below](#next-generation-configuration).
86
+
In AVA 3, for `ava.config.js` files you must use `export default`. You cannot use ["module scope"](https://nodejs.org/docs/latest-v12.x/api/modules.html#modules_the_module_scope). You cannot import dependencies.
85
87
86
-
The default export can either be a plain object or a factory function which returns a plain object:
88
+
The default export can either be a plain object or a factory function which returns a plain object. Starting in AVA 4 you can export or return a promise for a plain object:
Note that the final configuration must not be a promise. This is changing in AVA 4, [see below](#next-generation-configuration).
119
-
120
120
### `ava.config.cjs`
121
121
122
122
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.
123
123
124
-
The module export can either be a plain object or a factory function which returns a plain object:
124
+
The module export can either be a plain object or a factory function which returns a plain object. Starting in AVA 4 you can export or return a promise for a plain object:
Note that the final configuration must not be a promise. This is changing in AVA 4, [see below](#next-generation-configuration).
156
+
### `ava.config.mjs`
157
157
158
-
## Alternative configuration files
158
+
Note that `ava.config.mjs` files are only supported in AVA 4.
159
+
160
+
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:
161
+
162
+
```js
163
+
exportdefault {
164
+
require: ['./_my-test-helper']
165
+
};
166
+
```
167
+
168
+
```js
169
+
exportdefaultfunctionfactory() {
170
+
return {
171
+
require: ['./_my-test-helper']
172
+
};
173
+
};
174
+
```
175
+
176
+
The factory function is called with an object containing a `projectDir` property, which you could use to change the returned configuration:
177
+
178
+
```js
179
+
exportdefault ({projectDir}) => {
180
+
if (projectDir ==='/Users/username/projects/my-project') {
181
+
return {
182
+
// Config A
183
+
};
184
+
}
159
185
160
-
The [CLI] lets you specify a specific configuration file, using the `--config` flag. This file must have either a `.js` or `.cjs` extension and is processed like an `ava.config.js` or `ava.config.cjs` file would be.
186
+
return {
187
+
// Config B
188
+
};
189
+
};
190
+
```
161
191
162
-
AVA 4 also supports `.mjs` extensions, [see below](#next-generation-configuration).
192
+
## Alternative configuration files
193
+
194
+
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.
163
195
164
-
When the `--config` flag is set, the provided file will override all configuration from the `package.json` and `ava.config.js`or `ava.config.cjs` files. The configuration is not merged.
196
+
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.
165
197
166
-
The configuration file *must* be in the same directory as the `package.json` file.
198
+
Note: In AVA 3 the configuration file *must* be in the same directory as the `package.json` file. This restriction does not apply to AVA 4.
167
199
168
200
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:
169
201
@@ -188,25 +220,6 @@ module.exports = {
188
220
189
221
You can now run your unit tests through `npx ava` and the integration tests through `npx ava --config integration-tests.config.cjs`.
190
222
191
-
## Next generation configuration
192
-
193
-
AVA 4 will add full support for ESM configuration files as well as allowing you to have asynchronous factory functions. If you're using Node.js 12 or later you can opt-in to these features in AVA 3 by enabling the `nextGenConfig` experiment. Say in an `ava.config.mjs` file:
194
-
195
-
```js
196
-
exportdefault {
197
-
nonSemVerExperiments: {
198
-
nextGenConfig:true
199
-
},
200
-
files: ['unit-tests/**/*']
201
-
};
202
-
```
203
-
204
-
This also allows you to pass an `.mjs` file using the `--config` argument.
205
-
206
-
With this experiment enabled, AVA will no longer have special treatment for `ava.config.js` files. Instead AVA follows Node.js' behavior, so if you've set [`"type": "module"`](https://nodejs.org/docs/latest/api/packages.html#packages_type) you must use ESM, and otherwise you must use CommonJS.
207
-
208
-
You mustn't have an `ava.config.mjs` file next to an `ava.config.js` or `ava.config.cjs` file.
209
-
210
223
## Object printing depth
211
224
212
225
By default, AVA prints nested objects to a depth of `3`. However, when debugging tests with deeply nested objects, it can be useful to print with more detail. This can be done by setting [`util.inspect.defaultOptions.depth`](https://nodejs.org/api/util.html#util_util_inspect_defaultoptions) to the desired depth, before the test is executed:
0 commit comments