Skip to content

Commit 5b76a0a

Browse files
fix(jest): adjust conversion of CLI args to Jest args
This adjusts the conversion of CLI args (parsed by our `cli/parse-flags` module) to arguments for Jest to fix a problematic situation when passing an argument which is not recognized by our `parse-flags` module. A common pattern in Jest is to use a filename filter to narrow which tests are matched and run by the test runner, like so: ``` jest -- some-file-name.test.ts ``` Our CLI arg parsing module recognizes all Jest CLI args (like `--json`, `--watch`, and more) as 'first class' args and parses them into a `ConfigFlags` object. For the arguments shown above (`'--'`, `'some-file-name.test.ts'`), however, it will _not_ recognize them as first-class arguments and will instead preserve them in the `unknownArgs` array on the `ConfigFlags` object. The flags (known and unknown) parsed by the `parse-flags` module are then converted into settings for Jest using the `buildJestArgv` function. Prior to this commit we took the known and unknown args off of the `ConfigFlags` object like so: ```ts const args = [...config.flags.unknownArgs.slice(), ...config.flags.knownArgs.slice()]; ``` If a filename match pattern is used alone (as in the example above) this will result in an array that looks like this: ```ts const args = ['--', 'some-file-name.test.ts']; ``` That will translate correctly to what we want to communicate to Jest, and it will only run test files which match the pattern. The problem comes in when a filename match pattern is used in conjunction with a boolean flag, like so: ``` jest --json -- some-file-name.test.ts ``` Or, in a Stencil context, doing something like this: ``` npx stencil test --spec --json -- my-component.spec.ts ``` which would result in the following `args` array being passed to Jest: ```ts const args = [ '--', 'some-file-name.test.ts', '--spec', '--json', ]; ``` Jest expects that `'--'` is _only_ followed by filename patterns and that all boolean flags like `--json` are before it in the argument list, so it will interpret this array as trying to match filenames which contain the string `'--json'`, rather than setting the `.json` flag on the Jest config. The solution to this is to switch the order of the arguments passed to Jest, passing the known arguments _first_, followed by the unknown arguments. Note: this used to work properly before #3444 because a flag like `--json` would have been stuck in the `unknownArgs` array, so the argument array passed to Jest would have looked like: ```ts const args = [ '--json', '--', 'some-file-name.test.ts' ]; ``` When we added explicit support for all Jest args this behavior was broken. This commit adds a regression test which fails without the fix (changing the argument order) and adds the fix as well.
1 parent 7bb0a27 commit 5b76a0a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/testing/jest/jest-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function getLegacyJestOptions(): Record<string, boolean | number | string> {
4646
export function buildJestArgv(config: d.ValidatedConfig): Config.Argv {
4747
const yargs = require('yargs');
4848

49-
const args = [...config.flags.unknownArgs.slice(), ...config.flags.knownArgs.slice()];
49+
const args = [...config.flags.knownArgs.slice(), ...config.flags.unknownArgs.slice()];
5050

5151
if (!args.some((a) => a.startsWith('--max-workers') || a.startsWith('--maxWorkers'))) {
5252
args.push(`--max-workers=${config.maxConcurrentWorkers}`);

src/testing/jest/test/jest-config.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,16 @@ describe('jest-config', () => {
189189
expect(jestArgv.spec).toBe(true);
190190
expect(jestArgv.passWithNoTests).toBe(true);
191191
});
192+
193+
it('should parse a setup with a filepath constraint', () => {
194+
const args = ['test', '--spec', '--json', '--', 'my-component.spec.ts'];
195+
const config = mockValidatedConfig();
196+
config.flags = parseFlags(args);
197+
198+
expect(config.flags.args).toEqual(['--spec', '--json', '--', 'my-component.spec.ts']);
199+
expect(config.flags.unknownArgs).toEqual(['--', 'my-component.spec.ts']);
200+
201+
const jestArgv = buildJestArgv(config);
202+
expect(jestArgv.json).toBe(true);
203+
});
192204
});

0 commit comments

Comments
 (0)