From 5f8c9692d41b58d3706c61db9a33215294e70049 Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Wed, 31 Jul 2024 18:08:43 -0700 Subject: [PATCH] fix(testing): update Jest types (#5910) * fix(testing): update Jest types * add more tests * prettier * apply configuration changes to other Jest versions --- .../config/test/validate-testing.spec.ts | 18 ++++++++++++++++-- src/compiler/config/validate-testing.ts | 4 +++- src/declarations/stencil-public-compiler.ts | 15 ++++++++++----- .../jest/jest-27-and-under/jest-config.ts | 10 ++++++++++ src/testing/jest/jest-28/jest-config.ts | 10 ++++++++++ src/testing/jest/jest-29/jest-config.ts | 10 ++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/compiler/config/test/validate-testing.spec.ts b/src/compiler/config/test/validate-testing.spec.ts index cb7fb661b90..30b149f45e4 100644 --- a/src/compiler/config/test/validate-testing.spec.ts +++ b/src/compiler/config/test/validate-testing.spec.ts @@ -579,7 +579,7 @@ describe('validateTesting', () => { throw new Error('No testRegex was found in the Stencil TestingConfig. Failing test.'); } - testRegex = new RegExp(testRegexSetting); + testRegex = new RegExp(testRegexSetting[0]); }); describe('test.* extensions', () => { @@ -690,13 +690,27 @@ describe('validateTesting', () => { userConfig.flags = { ...flags, e2e: true }; userConfig.testing = { testMatch: undefined, + testRegex: ['/regexStr/'], + }; + + const { config } = validateConfig(userConfig, mockLoadConfigInit()); + + expect(config.testing.testMatch).toBeUndefined(); + expect(config.testing.testRegex).toEqual(['/regexStr/']); + }); + + it('transforms testRegex to an array if passed in as string', () => { + userConfig.flags = { ...flags, e2e: true }; + userConfig.testing = { + testMatch: undefined, + // @ts-expect-error invalid type because of type update testRegex: '/regexStr/', }; const { config } = validateConfig(userConfig, mockLoadConfigInit()); expect(config.testing.testMatch).toBeUndefined(); - expect(config.testing.testRegex).toBe('/regexStr/'); + expect(config.testing.testRegex).toEqual(['/regexStr/']); }); }); diff --git a/src/compiler/config/validate-testing.ts b/src/compiler/config/validate-testing.ts index 74a663c94cf..df0afde3e67 100644 --- a/src/compiler/config/validate-testing.ts +++ b/src/compiler/config/validate-testing.ts @@ -151,7 +151,9 @@ export const validateTesting = (config: d.ValidatedConfig, diagnostics: d.Diagno * - this regex case shall match file names such as `my-cmp.spec.ts`, `test.spec.ts` * - this regex case shall not match file names such as `attest.ts`, `bespec.ts` */ - testing.testRegex = '(/__tests__/.*|(\\.|/)(test|spec|e2e))\\.[jt]sx?$'; + testing.testRegex = ['(/__tests__/.*|(\\.|/)(test|spec|e2e))\\.[jt]sx?$']; + } else if (typeof testing.testRegex === 'string') { + testing.testRegex = [testing.testRegex]; } if (Array.isArray(testing.testMatch)) { diff --git a/src/declarations/stencil-public-compiler.ts b/src/declarations/stencil-public-compiler.ts index 1ca24cd5cc8..3ef6dc6daf0 100644 --- a/src/declarations/stencil-public-compiler.ts +++ b/src/declarations/stencil-public-compiler.ts @@ -1765,6 +1765,9 @@ export interface Testing { destroy(): Promise; } +export declare type Path = string; +export declare type TransformerConfig = [string, Record]; + /** * Options for initiating a run of Stencil tests (spec and/or end-to-end) */ @@ -1798,7 +1801,7 @@ export interface JestConfig { * By default, Jest runs all tests and produces all errors into the console upon completion. * The bail config option can be used here to have Jest stop running tests after the first failure. Default: false */ - bail?: boolean; + bail?: boolean | number; /** * The directory where Jest should store its cached dependency information. Jest attempts to scan your dependency tree once (up-front) @@ -1884,8 +1887,8 @@ export interface JestConfig { reporters?: any; resetMocks?: boolean; resetModules?: boolean; - resolver?: string; - restoreMocks?: string; + resolver?: Path | null; + restoreMocks?: boolean; rootDir?: string; roots?: any[]; runner?: string; @@ -1905,12 +1908,14 @@ export interface JestConfig { testMatch?: string[]; testPathIgnorePatterns?: string[]; testPreset?: string; - testRegex?: string; + testRegex?: string[]; testResultsProcessor?: string; testRunner?: string; testURL?: string; timers?: string; - transform?: { [key: string]: string }; + transform?: { + [regex: string]: Path | TransformerConfig; + }; transformIgnorePatterns?: any[]; unmockedModulePathPatterns?: any[]; verbose?: boolean; diff --git a/src/testing/jest/jest-27-and-under/jest-config.ts b/src/testing/jest/jest-27-and-under/jest-config.ts index c1013bc57e3..2d9eeb68c3d 100644 --- a/src/testing/jest/jest-27-and-under/jest-config.ts +++ b/src/testing/jest/jest-27-and-under/jest-config.ts @@ -153,6 +153,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string { if (stencilConfigTesting.verbose) { jestConfig.verbose = stencilConfigTesting.verbose; } + if (typeof stencilConfigTesting.bail !== 'undefined') { + jestConfig.bail = + typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0; + } + if (stencilConfigTesting.prettierPath) { + jestConfig.prettierPath = stencilConfigTesting.prettierPath; + } + if (stencilConfigTesting.restoreMocks) { + jestConfig.restoreMocks = stencilConfigTesting.restoreMocks; + } jestConfig.testRunner = new Jest27Stencil().getDefaultJestRunner(); diff --git a/src/testing/jest/jest-28/jest-config.ts b/src/testing/jest/jest-28/jest-config.ts index f0af3bef1d8..ba90b6cc916 100644 --- a/src/testing/jest/jest-28/jest-config.ts +++ b/src/testing/jest/jest-28/jest-config.ts @@ -117,6 +117,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string { if (stencilConfigTesting.verbose) { jestConfig.verbose = stencilConfigTesting.verbose; } + if (typeof stencilConfigTesting.bail !== 'undefined') { + jestConfig.bail = + typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0; + } + if (stencilConfigTesting.prettierPath) { + jestConfig.prettierPath = stencilConfigTesting.prettierPath; + } + if (stencilConfigTesting.restoreMocks) { + jestConfig.restoreMocks = stencilConfigTesting.restoreMocks; + } jestConfig.testRunner = new Jest28Stencil().getDefaultJestRunner(); diff --git a/src/testing/jest/jest-29/jest-config.ts b/src/testing/jest/jest-29/jest-config.ts index 34b84138a9f..e5ed86f1ed5 100644 --- a/src/testing/jest/jest-29/jest-config.ts +++ b/src/testing/jest/jest-29/jest-config.ts @@ -117,6 +117,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string { if (stencilConfigTesting.verbose) { jestConfig.verbose = stencilConfigTesting.verbose; } + if (typeof stencilConfigTesting.bail !== 'undefined') { + jestConfig.bail = + typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0; + } + if (stencilConfigTesting.prettierPath) { + jestConfig.prettierPath = stencilConfigTesting.prettierPath; + } + if (stencilConfigTesting.restoreMocks) { + jestConfig.restoreMocks = stencilConfigTesting.restoreMocks; + } jestConfig.testRunner = new Jest29Stencil().getDefaultJestRunner();