Skip to content

Commit

Permalink
Require --config files to be next to the package.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Jul 7, 2019
1 parent f6bb85c commit a2eb6db
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Note that the final configuration must not be a promise.

The [CLI] lets you specify a specific configuration file, using the `--config` flag. This file is processed just like an `ava.config.js` file would be. When the `--config` flag is set, the provided file will override all configuration from the `package.json` and `ava.config.js` files. The configuration is not merged.

The configuration file *must* be in the same directory as the `package.json` file.

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.js`:
Expand Down
13 changes: 11 additions & 2 deletions lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ const pkgConf = require('pkg-conf');
const NO_SUCH_FILE = Symbol('no ava.config.js file');
const MISSING_DEFAULT_EXPORT = Symbol('missing default export');

function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) {
function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) { // eslint-disable-line complexity
let packageConf = pkgConf.sync('ava', {cwd: resolveFrom});
const filepath = pkgConf.filepath(packageConf);
const projectDir = filepath === null ? resolveFrom : path.dirname(filepath);

const fileForErrorMessage = configFile || 'ava.config.js';
const allowConflictWithPackageJson = Boolean(configFile);

if (configFile) {
configFile = path.resolve(configFile); // Relative to CWD
if (path.basename(configFile) !== path.relative(projectDir, configFile)) {
throw new Error('Config files must be located next to the package.json file');
}
} else {
configFile = path.join(projectDir, 'ava.config.js');
}

let fileConf;
try {
({default: fileConf = MISSING_DEFAULT_EXPORT} = esm(module, {
Expand All @@ -29,7 +38,7 @@ function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {
},
force: true,
mode: 'all'
})(configFile ? path.resolve(configFile) : path.join(projectDir, 'ava.config.js')));
})(configFile));
} catch (error) {
if (error && error.code === 'MODULE_NOT_FOUND') {
fileConf = NO_SUCH_FILE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
files: 'package-yes-explicit-yes-nested-test-value'
};
6 changes: 6 additions & 0 deletions test/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ test('explicit configFile option overrides package.json config', t => {
t.end();
});

test('throws if configFile option is not in the same directory as the package.json file', t => {
changeDir('package-yes-explicit-yes');
t.throws(() => loadConfig({configFile: 'nested/explicit.js'}), /Config files must be located next to the package.json file/);
t.end();
});

test('merges in defaults passed with initial call', t => {
changeDir('package-only');
const defaults = {
Expand Down

0 comments on commit a2eb6db

Please sign in to comment.