Skip to content

Commit 5f8c969

Browse files
fix(testing): update Jest types (#5910)
* fix(testing): update Jest types * add more tests * prettier * apply configuration changes to other Jest versions
1 parent c3d4e8b commit 5f8c969

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

src/compiler/config/test/validate-testing.spec.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ describe('validateTesting', () => {
579579
throw new Error('No testRegex was found in the Stencil TestingConfig. Failing test.');
580580
}
581581

582-
testRegex = new RegExp(testRegexSetting);
582+
testRegex = new RegExp(testRegexSetting[0]);
583583
});
584584

585585
describe('test.* extensions', () => {
@@ -690,13 +690,27 @@ describe('validateTesting', () => {
690690
userConfig.flags = { ...flags, e2e: true };
691691
userConfig.testing = {
692692
testMatch: undefined,
693+
testRegex: ['/regexStr/'],
694+
};
695+
696+
const { config } = validateConfig(userConfig, mockLoadConfigInit());
697+
698+
expect(config.testing.testMatch).toBeUndefined();
699+
expect(config.testing.testRegex).toEqual(['/regexStr/']);
700+
});
701+
702+
it('transforms testRegex to an array if passed in as string', () => {
703+
userConfig.flags = { ...flags, e2e: true };
704+
userConfig.testing = {
705+
testMatch: undefined,
706+
// @ts-expect-error invalid type because of type update
693707
testRegex: '/regexStr/',
694708
};
695709

696710
const { config } = validateConfig(userConfig, mockLoadConfigInit());
697711

698712
expect(config.testing.testMatch).toBeUndefined();
699-
expect(config.testing.testRegex).toBe('/regexStr/');
713+
expect(config.testing.testRegex).toEqual(['/regexStr/']);
700714
});
701715
});
702716

src/compiler/config/validate-testing.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ export const validateTesting = (config: d.ValidatedConfig, diagnostics: d.Diagno
151151
* - this regex case shall match file names such as `my-cmp.spec.ts`, `test.spec.ts`
152152
* - this regex case shall not match file names such as `attest.ts`, `bespec.ts`
153153
*/
154-
testing.testRegex = '(/__tests__/.*|(\\.|/)(test|spec|e2e))\\.[jt]sx?$';
154+
testing.testRegex = ['(/__tests__/.*|(\\.|/)(test|spec|e2e))\\.[jt]sx?$'];
155+
} else if (typeof testing.testRegex === 'string') {
156+
testing.testRegex = [testing.testRegex];
155157
}
156158

157159
if (Array.isArray(testing.testMatch)) {

src/declarations/stencil-public-compiler.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,9 @@ export interface Testing {
17651765
destroy(): Promise<void>;
17661766
}
17671767

1768+
export declare type Path = string;
1769+
export declare type TransformerConfig = [string, Record<string, unknown>];
1770+
17681771
/**
17691772
* Options for initiating a run of Stencil tests (spec and/or end-to-end)
17701773
*/
@@ -1798,7 +1801,7 @@ export interface JestConfig {
17981801
* By default, Jest runs all tests and produces all errors into the console upon completion.
17991802
* The bail config option can be used here to have Jest stop running tests after the first failure. Default: false
18001803
*/
1801-
bail?: boolean;
1804+
bail?: boolean | number;
18021805

18031806
/**
18041807
* 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 {
18841887
reporters?: any;
18851888
resetMocks?: boolean;
18861889
resetModules?: boolean;
1887-
resolver?: string;
1888-
restoreMocks?: string;
1890+
resolver?: Path | null;
1891+
restoreMocks?: boolean;
18891892
rootDir?: string;
18901893
roots?: any[];
18911894
runner?: string;
@@ -1905,12 +1908,14 @@ export interface JestConfig {
19051908
testMatch?: string[];
19061909
testPathIgnorePatterns?: string[];
19071910
testPreset?: string;
1908-
testRegex?: string;
1911+
testRegex?: string[];
19091912
testResultsProcessor?: string;
19101913
testRunner?: string;
19111914
testURL?: string;
19121915
timers?: string;
1913-
transform?: { [key: string]: string };
1916+
transform?: {
1917+
[regex: string]: Path | TransformerConfig;
1918+
};
19141919
transformIgnorePatterns?: any[];
19151920
unmockedModulePathPatterns?: any[];
19161921
verbose?: boolean;

src/testing/jest/jest-27-and-under/jest-config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string {
153153
if (stencilConfigTesting.verbose) {
154154
jestConfig.verbose = stencilConfigTesting.verbose;
155155
}
156+
if (typeof stencilConfigTesting.bail !== 'undefined') {
157+
jestConfig.bail =
158+
typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0;
159+
}
160+
if (stencilConfigTesting.prettierPath) {
161+
jestConfig.prettierPath = stencilConfigTesting.prettierPath;
162+
}
163+
if (stencilConfigTesting.restoreMocks) {
164+
jestConfig.restoreMocks = stencilConfigTesting.restoreMocks;
165+
}
156166

157167
jestConfig.testRunner = new Jest27Stencil().getDefaultJestRunner();
158168

src/testing/jest/jest-28/jest-config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string {
117117
if (stencilConfigTesting.verbose) {
118118
jestConfig.verbose = stencilConfigTesting.verbose;
119119
}
120+
if (typeof stencilConfigTesting.bail !== 'undefined') {
121+
jestConfig.bail =
122+
typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0;
123+
}
124+
if (stencilConfigTesting.prettierPath) {
125+
jestConfig.prettierPath = stencilConfigTesting.prettierPath;
126+
}
127+
if (stencilConfigTesting.restoreMocks) {
128+
jestConfig.restoreMocks = stencilConfigTesting.restoreMocks;
129+
}
120130

121131
jestConfig.testRunner = new Jest28Stencil().getDefaultJestRunner();
122132

src/testing/jest/jest-29/jest-config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ export function buildJestConfig(config: d.ValidatedConfig): string {
117117
if (stencilConfigTesting.verbose) {
118118
jestConfig.verbose = stencilConfigTesting.verbose;
119119
}
120+
if (typeof stencilConfigTesting.bail !== 'undefined') {
121+
jestConfig.bail =
122+
typeof stencilConfigTesting.bail === 'number' ? stencilConfigTesting.bail : stencilConfigTesting.bail ? 1 : 0;
123+
}
124+
if (stencilConfigTesting.prettierPath) {
125+
jestConfig.prettierPath = stencilConfigTesting.prettierPath;
126+
}
127+
if (stencilConfigTesting.restoreMocks) {
128+
jestConfig.restoreMocks = stencilConfigTesting.restoreMocks;
129+
}
120130

121131
jestConfig.testRunner = new Jest29Stencil().getDefaultJestRunner();
122132

0 commit comments

Comments
 (0)