From 16459df18472a781a9af3dd17d3b7ffba7f6af11 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Thu, 27 Feb 2020 20:09:20 -0500 Subject: [PATCH] (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 --- src/index.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1ade12a95..8fc3b9a70 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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({