Skip to content

Commit 334e15b

Browse files
committed
Support require() in config files
1 parent 5751226 commit 334e15b

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
lines changed

docs/06-configuration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ Note that providing files on the CLI overrides the `files` option. If you've con
6969

7070
To use an `ava.config.js` file:
7171

72-
1. It must be in the same directory as your `package.json`
73-
2. Your `package.json` must not contain an `ava` property (or, if it does, it must be an empty object)
72+
1. It must be in the same directory as your `package.json`
73+
2. Your `package.json` must not contain an `ava` property (or, if it does, it must be an empty object)
74+
3. You must use `export default`, though [`require()`](https://nodejs.org/api/modules.html#modules_require_id) is available to load non-ES modules
7475

7576
The config file must have a default export, using ES modules. It can either be a plain object or a factory function which returns a plain object:
7677

lib/load-config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ function loadConfig(defaults = {}) {
1515
let fileConf;
1616
try {
1717
({default: fileConf = MISSING_DEFAULT_EXPORT} = esm(module, {
18-
cjs: false,
18+
cjs: {
19+
cache: false,
20+
extensions: false,
21+
interop: false,
22+
mutableNamespace: false,
23+
namedExports: false,
24+
paths: false,
25+
vars: true
26+
},
1927
force: true,
2028
mode: 'all'
2129
})(path.join(projectDir, 'ava.config.js')));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const config = require('./cjs');
2+
3+
export default config;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
files: 'config-file-cjs-test-value'
3+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/load-config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ test('loads config from factory function', t => {
5252
t.end();
5353
});
5454

55+
test('supports require() inside config file', t => {
56+
changeDir('require');
57+
const conf = loadConfig();
58+
t.is(conf.files, 'config-file-cjs-test-value');
59+
t.end();
60+
});
61+
5562
test('throws an error if a config factory returns a promise', t => {
5663
changeDir('factory-no-promise-return');
5764
t.throws(loadConfig);

0 commit comments

Comments
 (0)