Skip to content

Commit

Permalink
fix(testing): update Jest types (#5910)
Browse files Browse the repository at this point in the history
* fix(testing): update Jest types

* add more tests

* prettier

* apply configuration changes to other Jest versions
  • Loading branch information
christian-bromann authored Aug 1, 2024
1 parent c3d4e8b commit 5f8c969
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/compiler/config/test/validate-testing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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/']);
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/config/validate-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
15 changes: 10 additions & 5 deletions src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,9 @@ export interface Testing {
destroy(): Promise<void>;
}

export declare type Path = string;
export declare type TransformerConfig = [string, Record<string, unknown>];

/**
* Options for initiating a run of Stencil tests (spec and/or end-to-end)
*/
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/testing/jest/jest-27-and-under/jest-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 10 additions & 0 deletions src/testing/jest/jest-28/jest-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 10 additions & 0 deletions src/testing/jest/jest-29/jest-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 5f8c969

Please sign in to comment.