From 1e3f934ae67a24df06877bf40c65963f7f7f94d0 Mon Sep 17 00:00:00 2001 From: mararok <5163714+Mararok@users.noreply.github.com> Date: Thu, 29 Aug 2024 22:29:24 +0200 Subject: [PATCH] fix(testing): fix passing extra args to Jest cli --- packages/jest/src/executors/jest/jest.impl.spec.ts | 2 ++ packages/jest/src/executors/jest/jest.impl.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/jest/src/executors/jest/jest.impl.spec.ts b/packages/jest/src/executors/jest/jest.impl.spec.ts index bcc33a4b8245c0..ee9499d96bbe0f 100644 --- a/packages/jest/src/executors/jest/jest.impl.spec.ts +++ b/packages/jest/src/executors/jest/jest.impl.spec.ts @@ -127,6 +127,7 @@ describe('Jest Executor', () => { }, mockContext ); + expect(process.argv).toContain('--group=core'); expect(runCLI).toHaveBeenCalledWith( expect.objectContaining({ _: [], @@ -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 () => { diff --git a/packages/jest/src/executors/jest/jest.impl.ts b/packages/jest/src/executors/jest/jest.impl.ts index 2c8210d5cb89f1..f36e13134c35c3 100644 --- a/packages/jest/src/executors/jest/jest.impl.ts +++ b/packages/jest/src/executors/jest/jest.impl.ts @@ -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 }; } @@ -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);