Skip to content

Commit

Permalink
fix(testing): fix passing extra args to Jest cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok committed Oct 12, 2024
1 parent 37a7eb1 commit 1e3f934
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/jest/src/executors/jest/jest.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('Jest Executor', () => {
},
mockContext
);
expect(process.argv).toContain('--group=core');
expect(runCLI).toHaveBeenCalledWith(
expect.objectContaining({
_: [],
Expand All @@ -136,6 +137,7 @@ describe('Jest Executor', () => {
}),
['/root/jest.config.js']
);
process.argv.pop(); // clean extra arg.
});

it('should send appropriate options to jestCLI when testFile is specified', async () => {
Expand Down
14 changes: 13 additions & 1 deletion packages/jest/src/executors/jest/jest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ import { readFileSync } from 'fs';
import type { BatchResults } from 'nx/src/tasks-runner/batch/batch-messages';
process.env.NODE_ENV ??= 'test';

function injectExtraArgsToProcessArgv(extraArgs) {
if (typeof extraArgs === 'object') {
for (const arg in extraArgs) {
process.argv.push(`--${arg}=${extraArgs[arg]}`);
}
}
}

export async function jestExecutor(
options: JestExecutorOptions,
context: ExecutorContext
): Promise<{ success: boolean }> {
const config = await jestConfigParser(options, context);
injectExtraArgsToProcessArgv(config.extraArgs);

const { results } = await runCLI(config, [options.jestConfig]);

return { success: results.success };
}

Expand Down Expand Up @@ -97,6 +105,10 @@ export async function jestConfigParser(
randomize: options.randomize,
};

if (Object.keys(extraArgs).length > 0) {
config['extraArgs'] = extraArgs;
}

if (!multiProjects) {
options.jestConfig = path.resolve(context.root, options.jestConfig);

Expand Down

0 comments on commit 1e3f934

Please sign in to comment.