Skip to content

Commit

Permalink
(feat): support custom jest config paths via --config
Browse files Browse the repository at this point in the history
- --config will now be parsed shallow merged with the defaults,
  just like package.json.jest already is

- previously adding --config to tsdx test would result in jest
  outputting a usage prompt (I believe due to the second --config
  that's added internally) and then the somewhat cryptic
  "argv.config.match is not a function"
  - if --config is detected, it will be parsed, merged, and then
    deleted from argv so that this error doesn't occur anymore
  • Loading branch information
agilgur5 committed Mar 9, 2020
1 parent d164dd2 commit 16459df
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/index.ts
Expand Up @@ -487,7 +487,7 @@ prog
.describe(
'Run jest test runner in watch mode. Passes through all flags directly to Jest'
)
.action(async () => {
.action(async (opts: { config?: string }) => {
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';
Expand All @@ -508,12 +508,29 @@ prog
};

// Allow overriding with jest.config
const jestConfigExists = await fs.pathExists(paths.jestConfig);
if (jestConfigExists) {
const jestConfigContents = require(paths.jestConfig);
const defaultPathExists = await fs.pathExists(paths.jestConfig);
if (opts.config || defaultPathExists) {
const jestConfigPath = resolveApp(opts.config || paths.jestConfig);
const jestConfigContents = require(jestConfigPath);
jestConfig = { ...jestConfig, ...jestConfigContents };
}

// if custom path, delete the arg as it's already been merged
if (opts.config) {
let configIndex = argv.indexOf('--config');
if (configIndex !== -1) {
// case of "--config path", delete both args
argv.splice(configIndex, 2);
} else {
// case of "--config=path", only one arg to delete
const configRegex = /--config=.+/;
configIndex = argv.findIndex(arg => arg.match(configRegex));
if (configIndex !== -1) {
argv.splice(configIndex, 1);
}
}
}

argv.push(
'--config',
JSON.stringify({
Expand Down

0 comments on commit 16459df

Please sign in to comment.