Skip to content

Commit 16459df

Browse files
committed
(feat): support custom jest config paths via --config
- --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
1 parent d164dd2 commit 16459df

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ prog
487487
.describe(
488488
'Run jest test runner in watch mode. Passes through all flags directly to Jest'
489489
)
490-
.action(async () => {
490+
.action(async (opts: { config?: string }) => {
491491
// Do this as the first thing so that any code reading it knows the right env.
492492
process.env.BABEL_ENV = 'test';
493493
process.env.NODE_ENV = 'test';
@@ -508,12 +508,29 @@ prog
508508
};
509509

510510
// Allow overriding with jest.config
511-
const jestConfigExists = await fs.pathExists(paths.jestConfig);
512-
if (jestConfigExists) {
513-
const jestConfigContents = require(paths.jestConfig);
511+
const defaultPathExists = await fs.pathExists(paths.jestConfig);
512+
if (opts.config || defaultPathExists) {
513+
const jestConfigPath = resolveApp(opts.config || paths.jestConfig);
514+
const jestConfigContents = require(jestConfigPath);
514515
jestConfig = { ...jestConfig, ...jestConfigContents };
515516
}
516517

518+
// if custom path, delete the arg as it's already been merged
519+
if (opts.config) {
520+
let configIndex = argv.indexOf('--config');
521+
if (configIndex !== -1) {
522+
// case of "--config path", delete both args
523+
argv.splice(configIndex, 2);
524+
} else {
525+
// case of "--config=path", only one arg to delete
526+
const configRegex = /--config=.+/;
527+
configIndex = argv.findIndex(arg => arg.match(configRegex));
528+
if (configIndex !== -1) {
529+
argv.splice(configIndex, 1);
530+
}
531+
}
532+
}
533+
517534
argv.push(
518535
'--config',
519536
JSON.stringify({

0 commit comments

Comments
 (0)