Skip to content

Commit 70a3999

Browse files
Convert isValid() to validate()
1 parent a52fc2f commit 70a3999

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

packages/jest-config/src/normalize.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,23 +406,24 @@ const buildTestPathPatterns = (
406406

407407
const config = {rootDir};
408408
const testPathPatterns = new TestPathPatterns(patterns, config);
409-
if (!testPathPatterns.isValid()) {
410-
showTestPathPatternsError(testPathPatterns);
409+
410+
try {
411+
testPathPatterns.validate();
412+
} catch {
413+
clearLine(process.stdout);
414+
415+
// eslint-disable-next-line no-console
416+
console.log(
417+
chalk.red(
418+
` Invalid testPattern ${testPathPatterns.toPretty()} supplied. ` +
419+
'Running all tests instead.',
420+
),
421+
);
422+
411423
return new TestPathPatterns([], config);
412424
}
413-
return testPathPatterns;
414-
};
415-
416-
const showTestPathPatternsError = (testPathPatterns: TestPathPatterns) => {
417-
clearLine(process.stdout);
418425

419-
// eslint-disable-next-line no-console
420-
console.log(
421-
chalk.red(
422-
` Invalid testPattern ${testPathPatterns.toPretty()} supplied. ` +
423-
'Running all tests instead.',
424-
),
425-
);
426+
return testPathPatterns;
426427
};
427428

428429
function validateExtensionsToTreatAsEsm(

packages/jest-util/src/TestPathPatterns.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class TestPathPatterns {
5454
return regexString;
5555
}
5656

57-
private get regex(): RegExp {
57+
private toRegex(): RegExp {
5858
return new RegExp(this.regexString, 'i');
5959
}
6060

@@ -66,26 +66,19 @@ export default class TestPathPatterns {
6666
}
6767

6868
/**
69-
* Return true if the patterns form a valid regex.
69+
* Throw an error if the patterns don't form a valid regex.
7070
*/
71-
isValid(): boolean {
72-
try {
73-
// @ts-expect-error noUnusedLocals
74-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
75-
const _ = this.regex;
76-
return true;
77-
} catch {
78-
return false;
79-
}
71+
validate(): void {
72+
this.toRegex();
8073
}
8174

8275
/**
8376
* Return true if the given ABSOLUTE path matches the patterns.
8477
*
85-
* Throws an error if the patterns form an invalid regex (see `isValid`).
78+
* Throws an error if the patterns form an invalid regex (see `validate`).
8679
*/
8780
isMatch(path: string): boolean {
88-
return this.regex.test(path);
81+
return this.toRegex().test(path);
8982
}
9083

9184
/**

packages/jest-util/src/__tests__/TestPathPatterns.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,25 @@ describe('TestPathPatterns', () => {
3636
});
3737
});
3838

39-
describe('isValid', () => {
40-
it('returns true for empty patterns', () => {
39+
describe('validate', () => {
40+
it('succeeds for empty patterns', () => {
4141
const testPathPatterns = new TestPathPatterns([], config);
42-
expect(testPathPatterns.isValid()).toBe(true);
42+
expect(() => testPathPatterns.validate()).not.toThrow();
4343
});
4444

45-
it('returns true for valid patterns', () => {
45+
it('succeeds for valid patterns', () => {
4646
const testPathPatterns = new TestPathPatterns(['abc+', 'z.*'], config);
47-
expect(testPathPatterns.isValid()).toBe(true);
47+
expect(() => testPathPatterns.validate()).not.toThrow();
4848
});
4949

50-
it('returns false for at least one invalid pattern', () => {
50+
it('fails for at least one invalid pattern', () => {
5151
const testPathPatterns = new TestPathPatterns(
5252
['abc+', '(', 'z.*'],
5353
config,
5454
);
55-
expect(testPathPatterns.isValid()).toBe(false);
55+
expect(() => testPathPatterns.validate()).toThrow(
56+
'Invalid regular expression',
57+
);
5658
});
5759
});
5860

0 commit comments

Comments
 (0)