Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): update Jest types #5910

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines +156 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to apply these to the other adapter versions as well?


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