Skip to content

Commit

Permalink
Convert isValid() to validate()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Sep 25, 2023
1 parent a52fc2f commit 70a3999
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
29 changes: 15 additions & 14 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,23 +406,24 @@ const buildTestPathPatterns = (

const config = {rootDir};
const testPathPatterns = new TestPathPatterns(patterns, config);
if (!testPathPatterns.isValid()) {
showTestPathPatternsError(testPathPatterns);

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

// eslint-disable-next-line no-console
console.log(
chalk.red(
` Invalid testPattern ${testPathPatterns.toPretty()} supplied. ` +
'Running all tests instead.',
),
);

return new TestPathPatterns([], config);
}
return testPathPatterns;
};

const showTestPathPatternsError = (testPathPatterns: TestPathPatterns) => {
clearLine(process.stdout);

// eslint-disable-next-line no-console
console.log(
chalk.red(
` Invalid testPattern ${testPathPatterns.toPretty()} supplied. ` +
'Running all tests instead.',
),
);
return testPathPatterns;
};

function validateExtensionsToTreatAsEsm(
Expand Down
19 changes: 6 additions & 13 deletions packages/jest-util/src/TestPathPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class TestPathPatterns {
return regexString;
}

private get regex(): RegExp {
private toRegex(): RegExp {
return new RegExp(this.regexString, 'i');
}

Expand All @@ -66,26 +66,19 @@ export default class TestPathPatterns {
}

/**
* Return true if the patterns form a valid regex.
* Throw an error if the patterns don't form a valid regex.
*/
isValid(): boolean {
try {
// @ts-expect-error noUnusedLocals
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _ = this.regex;
return true;
} catch {
return false;
}
validate(): void {
this.toRegex();
}

/**
* Return true if the given ABSOLUTE path matches the patterns.
*
* Throws an error if the patterns form an invalid regex (see `isValid`).
* Throws an error if the patterns form an invalid regex (see `validate`).
*/
isMatch(path: string): boolean {
return this.regex.test(path);
return this.toRegex().test(path);
}

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

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

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

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

Expand Down

0 comments on commit 70a3999

Please sign in to comment.