diff --git a/docs/06-configuration.md b/docs/06-configuration.md index bfa203ec8..5206bfe09 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -69,8 +69,9 @@ Note that providing files on the CLI overrides the `files` option. If you've con To use an `ava.config.js` file: - 1. It must be in the same directory as your `package.json` - 2. Your `package.json` must not contain an `ava` property (or, if it does, it must be an empty object) +1. It must be in the same directory as your `package.json` +2. Your `package.json` must not contain an `ava` property (or, if it does, it must be an empty object) +3. You must use `export default`, though [`require()`](https://nodejs.org/api/modules.html#modules_require_id) is available to load non-ES modules 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: diff --git a/lib/load-config.js b/lib/load-config.js index 03789f933..4ad33f135 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -15,7 +15,15 @@ function loadConfig(defaults = {}) { let fileConf; try { ({default: fileConf = MISSING_DEFAULT_EXPORT} = esm(module, { - cjs: false, + cjs: { + cache: false, + extensions: false, + interop: false, + mutableNamespace: false, + namedExports: false, + paths: false, + vars: true + }, force: true, mode: 'all' })(path.join(projectDir, 'ava.config.js'))); diff --git a/test/fixture/load-config/require/ava.config.js b/test/fixture/load-config/require/ava.config.js new file mode 100644 index 000000000..a7ea951d0 --- /dev/null +++ b/test/fixture/load-config/require/ava.config.js @@ -0,0 +1,3 @@ +const config = require('./cjs'); + +export default config; diff --git a/test/fixture/load-config/require/cjs.js b/test/fixture/load-config/require/cjs.js new file mode 100644 index 000000000..5ecc07bbd --- /dev/null +++ b/test/fixture/load-config/require/cjs.js @@ -0,0 +1,3 @@ +module.exports = { + files: 'config-file-cjs-test-value' +}; diff --git a/test/fixture/load-config/require/package.json b/test/fixture/load-config/require/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/fixture/load-config/require/package.json @@ -0,0 +1 @@ +{} diff --git a/test/load-config.js b/test/load-config.js index bdcd533ef..c15413f50 100644 --- a/test/load-config.js +++ b/test/load-config.js @@ -52,6 +52,13 @@ test('loads config from factory function', t => { t.end(); }); +test('supports require() inside config file', t => { + changeDir('require'); + const conf = loadConfig(); + t.is(conf.files, 'config-file-cjs-test-value'); + t.end(); +}); + test('throws an error if a config factory returns a promise', t => { changeDir('factory-no-promise-return'); t.throws(loadConfig);