Skip to content

Commit

Permalink
Factor out toString() call
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Sep 21, 2023
1 parent b4c9587 commit 344bc43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 7 additions & 7 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1631,13 +1631,6 @@ describe('testPathPattern', () => {
expect(options.testPathPattern).toBe('a/b|c/d');
});

it('coerces all patterns to strings', async () => {
const argv = {[opt.property]: [1]} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('1');
});

describe('posix', () => {
it('should not escape the pattern', async () => {
const argv = {
Expand Down Expand Up @@ -1694,6 +1687,13 @@ describe('testPathPattern', () => {
});
}

it('coerces <regexForTestFiles> patterns to strings', async () => {
const argv = {_: [1]} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPattern).toBe('1');
});

it('joins multiple --testPathPatterns and <regexForTestFiles>', async () => {
const {options} = await normalize(initialOptions, {
_: ['a', 'b'],
Expand Down
12 changes: 3 additions & 9 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,14 @@ const buildTestPathPattern = (argv: Config.Argv): string => {
const patterns = [];

if (argv._) {
patterns.push(...argv._);
patterns.push(...argv._.map(x => x.toString()));
}
if (argv.testPathPattern) {
patterns.push(...argv.testPathPattern);
}

const replacePosixSep = (pattern: string | number) => {
// yargs coerces positional args into numbers
const patternAsString = pattern.toString();
if (path.sep === '/') {
return patternAsString;
}
return patternAsString.replace(/\//g, '\\\\');
};
const replacePosixSep = (pattern: string) =>
path.sep === '/' ? pattern : pattern.replace(/\//g, '\\\\');

const testPathPattern = patterns.map(replacePosixSep).join('|');
if (validatePattern(testPathPattern)) {
Expand Down

0 comments on commit 344bc43

Please sign in to comment.