Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change TestPathPatterns.{validate => isValid}
  • Loading branch information
brandonchinn178 committed Mar 7, 2024
commit c124aaf7e03d4e227d612035e04897fc3a95d3d4
4 changes: 1 addition & 3 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,7 @@ const buildTestPathPatterns = (
const config = {rootDir};
const testPathPatterns = new TestPathPatterns(patterns, config);

try {
testPathPatterns.validate();
} catch {
if (!testPathPatterns.isValid()) {
clearLine(process.stdout);

// eslint-disable-next-line no-console
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-types/src/TestPathPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ export default class TestPathPatterns {
}

/**
* Throw an error if the patterns don't form a valid regex.
* Return true if the patterns are valid.
*/
validate(): void {
this.toRegex();
isValid(): boolean {
try {
this.toRegex();
return true;
} catch {
return false;
}
}

/**
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-types/src/__tests__/TestPathPatterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,23 @@ describe('TestPathPatterns', () => {
});
});

describe('validate', () => {
describe('isValid', () => {
it('succeeds for empty patterns', () => {
const testPathPatterns = new TestPathPatterns([], config);
expect(() => testPathPatterns.validate()).not.toThrow();
expect(testPathPatterns.isValid()).toBe(true);
});

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

it('fails for at least one invalid pattern', () => {
const testPathPatterns = new TestPathPatterns(
['abc+', '(', 'z.*'],
config,
);
expect(() => testPathPatterns.validate()).toThrow(
'Invalid regular expression',
);
expect(testPathPatterns.isValid()).toBe(false);
});
});

Expand Down