Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixes

* `[jest-config]` Escape Windows path separator in testPathPattern CLI arguments
([#5054](https://github.com/facebook/jest/pull/5054)
* `[jest-jasmine]` Register sourcemaps as node environment to improve performance with jsdom ([#5045](https://github.com/facebook/jest/pull/5045))
* `[pretty-format]` Do not call toJSON recursively
([#5044](https://github.com/facebook/jest/pull/5044))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = `
<red> https://facebook.github.io/jest/docs/configuration.html</>
<red></>"
`;

exports[`testPathPattern <regexForTestFiles> ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;

exports[`testPathPattern --testPathPattern ignores invalid regular expressions and logs a warning 1`] = `"<red> Invalid testPattern a( supplied. Running all tests instead.</>"`;
70 changes: 70 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,3 +1026,73 @@ describe('watchPlugins', () => {
]);
});
});

describe('testPathPattern', () => {
const initialOptions = {rootDir: '/root'};
const consoleLog = console.log;

function testWindowsPathSeparator(argv, expected) {
jest.resetModules();
jest.mock('path', () => require.requireActual('path').win32);
require('jest-resolve').findNodeModule = findNodeModule;

const {options} = require('../normalize').default(initialOptions, argv);
expect(options.testPathPattern).toBe(expected);
}

beforeEach(() => {
console.log = jest.fn();
});

afterEach(() => {
console.log = consoleLog;
});

it('defaults to empty', () => {
const {options} = normalize(initialOptions, {});
expect(options.testPathPattern).toBe('');
});

describe('--testPathPattern', () => {
it('uses testPathPattern if set', () => {
const {options} = normalize(initialOptions, {testPathPattern: 'a/b'});
expect(options.testPathPattern).toBe('a/b');
});

it('ignores invalid regular expressions and logs a warning', () => {
const {options} = normalize(initialOptions, {testPathPattern: 'a('});
expect(options.testPathPattern).toBe('');
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
});

it('escapes Windows path separators', () => {
testWindowsPathSeparator({testPathPattern: 'a\\b'}, 'a\\\\b');
});
});

describe('<regexForTestFiles>', () => {
it('uses <regexForTestFiles> if set', () => {
const {options} = normalize(initialOptions, {_: ['a/b']});
expect(options.testPathPattern).toBe('a/b');
});

it('ignores invalid regular expressions and logs a warning', () => {
const {options} = normalize(initialOptions, {_: ['a(']});
expect(options.testPathPattern).toBe('');
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
});

it('escapes Windows path separators', () => {
testWindowsPathSeparator({_: ['a\\b']}, 'a\\\\b');
});

it('joins multiple <regexForTestFiles> if set', () => {
const {options} = normalize(initialOptions, {_: ['a/b', 'c/d']});
expect(options.testPathPattern).toBe('a/b|c/d');
});

it('escapes Windows path separators in multiple args', () => {
testWindowsPathSeparator({_: ['a\\b', 'c\\d']}, 'a\\\\b|c\\\\d');
});
});
});
4 changes: 2 additions & 2 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ const normalizeReporters = (options: InitialOptions, basedir) => {
const buildTestPathPattern = (argv: Argv): string => {
if (argv.testPathPattern) {
if (validatePattern(argv.testPathPattern)) {
return argv.testPathPattern;
return replacePathSepForRegex(argv.testPathPattern);
} else {
showTestPathPatternError(argv.testPathPattern);
}
}

if (argv._ && argv._.length) {
const testPathPattern = argv._.join('|');
const testPathPattern = argv._.map(replacePathSepForRegex).join('|');

if (validatePattern(testPathPattern)) {
return testPathPattern;
Expand Down