Skip to content

Commit

Permalink
Support require() in config files
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Mar 3, 2019
1 parent 5751226 commit 334e15b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 9 additions & 1 deletion lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down
3 changes: 3 additions & 0 deletions test/fixture/load-config/require/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const config = require('./cjs');

export default config;
3 changes: 3 additions & 0 deletions test/fixture/load-config/require/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
files: 'config-file-cjs-test-value'
};
1 change: 1 addition & 0 deletions test/fixture/load-config/require/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions test/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 334e15b

Please sign in to comment.