Skip to content

Commit

Permalink
Fix testPathPatterns when config is in subdirectory (#14934)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 authored May 29, 2024
1 parent cb15c34 commit 68dca2a
Show file tree
Hide file tree
Showing 65 changed files with 405 additions and 145 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
- `[jest-runtime]` Properly handle re-exported native modules in ESM via CJS ([#14589](https://github.com/jestjs/jest/pull/14589))
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
- `[jest-util]` Add missing dependency on `jest-regex-util` ([#15030](https://github.com/jestjs/jest/pull/15030))
- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/jestjs/jest/pull/14290))
- `[jest-cli]` When specifying paths on the command line, only match against the relative paths of the test files ([#12519](https://github.com/jestjs/jest/pull/12519))
- [**BREAKING**] Changes `testPathPattern` configuration option to `testPathPatterns`, which now takes a list of patterns instead of the regex.
- [**BREAKING**] `--testPathPattern` is now `--testPathPatterns`
- [**BREAKING**] Specifying `testPathPatterns` when programmatically calling `watch` must be specified as `new TestPathPatterns(patterns)`, where `TestPathPatterns` can be imported from `@jest/pattern`
- `[jest-reporters, jest-runner]` Unhandled errors without stack get correctly logged to console ([#14619](https://github.com/jestjs/jest/pull/14619))

### Performance
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/executeTestsOnceInMpr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('Tests are executed only once even in an MPR', () => {
});
/* eslint-enable sort-keys */

const {stderr, exitCode} = runJest(DIR, ['foo/folder/my-test-bar.js']);
const {stderr, exitCode} = runJest(DIR, ['my-test-bar.js']);

expect(exitCode).toBe(0);

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/globalSetup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test('should not call a globalSetup of a project if there are no tests to run fr

const result = runWithJson(e2eDir, [
`--config=${configPath}`,
'--testPathPatterns=project-1',
'--testPathPatterns=setup1',
]);

expect(result.exitCode).toBe(0);
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/globalTeardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test('should not call a globalTeardown of a project if there are no tests to run

const result = runWithJson('global-teardown', [
`--config=${configPath}`,
'--testPathPatterns=project-1',
'--testPathPatterns=teardown1',
]);

expect(result.exitCode).toBe(0);
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/testPathPatternsSubprojects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {json} from '../runJest';

it('works when specifying --testPathPatterns when config is in subdir', () => {
const {
json: {numTotalTests},
} = json('test-path-patterns-subprojects', [
'--config=config/jest.config.js',
'--testPathPatterns=testA',
]);
expect(numTotalTests).toBe(1);
});
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion e2e/global-setup/setupWithConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*/

module.exports = function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPatterns);
console.log(globalConfig.testPathPatterns.patterns);
console.log(projectConfig.cache);
};
2 changes: 1 addition & 1 deletion e2e/global-setup/setupWithDefaultExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*/

export default function (globalConfig, projectConfig): void {
console.log(globalConfig.testPathPatterns);
console.log(globalConfig.testPathPatterns.patterns);
console.log(projectConfig.cache);
}
2 changes: 1 addition & 1 deletion e2e/global-teardown/teardownWithConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*/

module.exports = function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPatterns);
console.log(globalConfig.testPathPatterns.patterns);
console.log(projectConfig.cache);
};
2 changes: 1 addition & 1 deletion e2e/global-teardown/teardownWithDefaultExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*/

export default function (globalConfig, projectConfig): void {
console.log(globalConfig.testPathPatterns);
console.log(globalConfig.testPathPatterns.patterns);
console.log(projectConfig.cache);
}
8 changes: 7 additions & 1 deletion e2e/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dedent from 'dedent';
import execa = require('execa');
import * as fs from 'graceful-fs';
import stripAnsi = require('strip-ansi');
import {TestPathPatterns} from '@jest/pattern';
import type {FormattedTestResults} from '@jest/test-result';
import {normalizeIcons} from '@jest/test-utils';
import type {Config} from '@jest/types';
Expand Down Expand Up @@ -285,5 +286,10 @@ export function getConfig(
throw error;
}

return JSON.parse(stdout);
const {testPathPatterns, ...globalConfig} = JSON.parse(stdout);

return {
...globalConfig,
testPathPatterns: new TestPathPatterns(testPathPatterns),
};
}
10 changes: 10 additions & 0 deletions e2e/test-path-patterns-subprojects/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
projects: [{rootDir: 'src'}],
};
1 change: 1 addition & 0 deletions e2e/test-path-patterns-subprojects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
10 changes: 10 additions & 0 deletions e2e/test-path-patterns-subprojects/src/__tests__/testA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

it('test', () => {});
10 changes: 10 additions & 0 deletions e2e/test-path-patterns-subprojects/src/__tests__/testB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

it('test', () => {});
10 changes: 10 additions & 0 deletions e2e/test-path-patterns-subprojects/src/__tests__/testC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

it('test', () => {});
2 changes: 1 addition & 1 deletion packages/jest-cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ export const options: {[key: string]: Options} = {
},
testPathPatterns: {
description:
'A regexp pattern string that is matched against all tests ' +
'An array of regexp pattern strings that are matched against all tests ' +
'paths before executing the test.',
requiresArg: true,
string: true,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"@babel/core": "^7.11.6",
"@jest/pattern": "workspace:*",
"@jest/test-sequencer": "workspace:*",
"@jest/types": "workspace:*",
"babel-jest": "workspace:*",
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,7 +1601,7 @@ describe('testPathPatterns', () => {
it('defaults to empty', async () => {
const {options} = await normalize(initialOptions, {} as Config.Argv);

expect(options.testPathPatterns).toEqual([]);
expect(options.testPathPatterns.patterns).toEqual([]);
});

const cliOptions = [
Expand All @@ -1614,22 +1614,22 @@ describe('testPathPatterns', () => {
const argv = {[opt.property]: ['a/b']} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPatterns).toEqual(['a/b']);
expect(options.testPathPatterns.patterns).toEqual(['a/b']);
});

it('ignores invalid regular expressions and logs a warning', async () => {
const argv = {[opt.property]: ['a(']} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPatterns).toEqual([]);
expect(options.testPathPatterns.patterns).toEqual([]);
expect(jest.mocked(console.log).mock.calls[0][0]).toMatchSnapshot();
});

it(`joins multiple ${opt.name} if set`, async () => {
const argv = {[opt.property]: ['a/b', 'c/d']} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPatterns).toEqual(['a/b', 'c/d']);
expect(options.testPathPatterns.patterns).toEqual(['a/b', 'c/d']);
});
});
}
Expand All @@ -1638,15 +1638,15 @@ describe('testPathPatterns', () => {
const argv = {_: [1]} as Config.Argv;
const {options} = await normalize(initialOptions, argv);

expect(options.testPathPatterns).toEqual(['1']);
expect(options.testPathPatterns.patterns).toEqual(['1']);
});

it('joins multiple --testPathPatterns and <regexForTestFiles>', async () => {
const {options} = await normalize(initialOptions, {
_: ['a', 'b'],
testPathPatterns: ['c', 'd'],
} as Config.Argv);
expect(options.testPathPatterns).toEqual(['a', 'b', 'c', 'd']);
expect(options.testPathPatterns.patterns).toEqual(['a', 'b', 'c', 'd']);
});

it('gives precedence to --all', async () => {
Expand Down
20 changes: 7 additions & 13 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import merge = require('deepmerge');
import {glob} from 'glob';
import {statSync} from 'graceful-fs';
import micromatch = require('micromatch');
import {TestPathPatterns} from '@jest/pattern';
import type {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';
import Resolver, {
Expand All @@ -22,7 +23,6 @@ import Resolver, {
resolveWatchPlugin,
} from 'jest-resolve';
import {
TestPathPatterns,
clearLine,
replacePathSepForGlob,
requireOrImportModule,
Expand Down Expand Up @@ -393,10 +393,7 @@ const normalizeReporters = ({
});
};

const buildTestPathPatterns = (
argv: Config.Argv,
rootDir: string,
): TestPathPatterns => {
const buildTestPathPatterns = (argv: Config.Argv): TestPathPatterns => {
const patterns = [];

if (argv._) {
Expand All @@ -406,12 +403,9 @@ const buildTestPathPatterns = (
patterns.push(...argv.testPathPatterns);
}

const config = {rootDir};
const testPathPatterns = new TestPathPatterns(patterns, config);
const testPathPatterns = new TestPathPatterns(patterns);

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

// eslint-disable-next-line no-console
Expand All @@ -422,7 +416,7 @@ const buildTestPathPatterns = (
),
);

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

return testPathPatterns;
Expand Down Expand Up @@ -1012,8 +1006,8 @@ export default async function normalize(
}

newOptions.nonFlagArgs = argv._?.map(arg => `${arg}`);
const testPathPatterns = buildTestPathPatterns(argv, options.rootDir);
newOptions.testPathPatterns = testPathPatterns.patterns;
const testPathPatterns = buildTestPathPatterns(argv);
newOptions.testPathPatterns = testPathPatterns;
newOptions.json = !!argv.json;

newOptions.testFailureExitCode = Number.parseInt(
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"references": [
{"path": "../jest-environment-node"},
{"path": "../jest-get-type"},
{"path": "../jest-pattern"},
{"path": "../jest-regex-util"},
{"path": "../jest-resolve"},
{"path": "../jest-runner"},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@jest/console": "workspace:*",
"@jest/pattern": "workspace:*",
"@jest/reporters": "workspace:*",
"@jest/test-result": "workspace:*",
"@jest/transform": "workspace:*",
Expand Down
Loading

0 comments on commit 68dca2a

Please sign in to comment.