From 9f942082436eb5280ef07e4cc8cee614b52980c1 Mon Sep 17 00:00:00 2001 From: Nikita Zyulyaev Date: Thu, 22 Jun 2023 15:05:26 +0100 Subject: [PATCH 1/4] Fix extra args not being passed to jest --- src/util/getCliOptions.test.ts | 7 +++++++ src/util/getCliOptions.ts | 6 ++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/util/getCliOptions.test.ts b/src/util/getCliOptions.test.ts index ae4d2726..dd300f49 100644 --- a/src/util/getCliOptions.test.ts +++ b/src/util/getCliOptions.test.ts @@ -10,4 +10,11 @@ describe('getCliOptions', () => { const opts = getCliOptions(); expect(opts.runnerOptions).toMatchObject(customConfig); }); + + it('returns extra args if passed', () => { + const extraArgs = ['TestName', 'AnotherTestName']; + jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValue({ options: {}, extraArgs }); + const opts = getCliOptions(); + expect(opts.jestOptions).toEqual(extraArgs); + }); }); diff --git a/src/util/getCliOptions.ts b/src/util/getCliOptions.ts index 9e098a5b..842014d0 100644 --- a/src/util/getCliOptions.ts +++ b/src/util/getCliOptions.ts @@ -1,9 +1,7 @@ import { getParsedCliOptions } from './getParsedCliOptions'; import type { BrowserType } from 'jest-playwright-preset'; -export type JestOptions = { - [key: string]: any; -}; +export type JestOptions = any[]; export type CliOptions = { runnerOptions: { @@ -63,7 +61,7 @@ export const getCliOptions = (): CliOptions => { }, defaultOptions); if (extraArgs.length) { - finalOptions.jestOptions.push(...[extraArgs]); + finalOptions.jestOptions.push(...extraArgs); } return finalOptions; From 4c0b9494ea5db8ba22b9c54da3909339c79550ea Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Thu, 13 Jul 2023 17:56:46 +0200 Subject: [PATCH 2/4] fix type --- src/util/getCliOptions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/getCliOptions.ts b/src/util/getCliOptions.ts index 842014d0..fd0371ff 100644 --- a/src/util/getCliOptions.ts +++ b/src/util/getCliOptions.ts @@ -1,7 +1,7 @@ import { getParsedCliOptions } from './getParsedCliOptions'; import type { BrowserType } from 'jest-playwright-preset'; -export type JestOptions = any[]; +export type JestOptions = string[]; export type CliOptions = { runnerOptions: { From 7ec911f972e1c983c82e2707412d2257d3dcf076 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Fri, 14 Jul 2023 10:10:48 +0200 Subject: [PATCH 3/4] only mock return values once --- src/util/getCliOptions.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/getCliOptions.test.ts b/src/util/getCliOptions.test.ts index dd300f49..18c2c2d6 100644 --- a/src/util/getCliOptions.test.ts +++ b/src/util/getCliOptions.test.ts @@ -6,14 +6,14 @@ describe('getCliOptions', () => { const customConfig = { configDir: 'custom', indexJson: true }; jest .spyOn(cliHelper, 'getParsedCliOptions') - .mockReturnValue({ options: customConfig, extraArgs: [] }); + .mockReturnValueOnce({ options: customConfig, extraArgs: [] }); const opts = getCliOptions(); expect(opts.runnerOptions).toMatchObject(customConfig); }); it('returns extra args if passed', () => { const extraArgs = ['TestName', 'AnotherTestName']; - jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValue({ options: {}, extraArgs }); + jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValueOnce({ options: {}, extraArgs }); const opts = getCliOptions(); expect(opts.jestOptions).toEqual(extraArgs); }); From 051dea340316627e23379a1b7cdcf499e11e4443 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Fri, 14 Jul 2023 14:56:45 +0200 Subject: [PATCH 4/4] mock process.argv in test --- src/util/getCliOptions.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/util/getCliOptions.test.ts b/src/util/getCliOptions.test.ts index 18c2c2d6..11ae3106 100644 --- a/src/util/getCliOptions.test.ts +++ b/src/util/getCliOptions.test.ts @@ -2,6 +2,12 @@ import { getCliOptions } from './getCliOptions'; import * as cliHelper from './getParsedCliOptions'; describe('getCliOptions', () => { + let originalArgv: string[] = process.argv; + + afterEach(() => { + process.argv = originalArgv; + }); + it('returns custom options if passed', () => { const customConfig = { configDir: 'custom', indexJson: true }; jest @@ -13,6 +19,9 @@ describe('getCliOptions', () => { it('returns extra args if passed', () => { const extraArgs = ['TestName', 'AnotherTestName']; + // mock argv to avoid side effect from running tests e.g. jest --coverage, + // which would end up caught by getCliOptions + process.argv = []; jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValueOnce({ options: {}, extraArgs }); const opts = getCliOptions(); expect(opts.jestOptions).toEqual(extraArgs);