Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only match relative paths when specifying paths #12519

Merged
merged 23 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
344bc43
Factor out toString() call
brandonchinn178 Apr 26, 2023
317d079
Remove redundant 'globalConfig.testPathPattern == null' check
brandonchinn178 Apr 26, 2023
86993d4
Consolidate testPathPatterns logic
brandonchinn178 Apr 26, 2023
ecd979f
Return TestPathPatterns in normalize helper
brandonchinn178 Apr 26, 2023
4caaa45
Store parsed patterns in global config
brandonchinn178 Apr 26, 2023
225b6fe
Change normalize tests to test testPathPatterns
brandonchinn178 Apr 26, 2023
993fc50
Replace testPathPatternToRegExp with TestPathPatterns
brandonchinn178 Apr 26, 2023
518c357
Update testPathPatterns in updateGlobalConfig
brandonchinn178 Apr 26, 2023
e347e72
Update docs + tests to use globalConfig.testPathPatterns
brandonchinn178 Apr 26, 2023
6713c2c
Replace remaining usages of globalConfig.testPathPattern
brandonchinn178 Sep 21, 2023
a4efd87
Remove testPathPattern from GlobalConfig
brandonchinn178 Apr 26, 2023
ae8ee58
Add failing tests for matching outside relative path
brandonchinn178 Apr 26, 2023
1911750
Only match within relative path
brandonchinn178 Apr 26, 2023
5fd3eda
Fix tests using partial GlobalConfigs
brandonchinn178 Apr 26, 2023
0225d22
Update CHANGELOG
brandonchinn178 Apr 26, 2023
e228478
Fix --testPathPattern => --testPathPatterns
brandonchinn178 Apr 26, 2023
de9edca
Don't overload TestPathPatterns constructor
brandonchinn178 Sep 25, 2023
042fddd
Update docs
brandonchinn178 Sep 25, 2023
a52fc2f
Add deprecation error for --testPathPattern
brandonchinn178 Sep 25, 2023
70a3999
Convert isValid() to validate()
brandonchinn178 Sep 25, 2023
3c22ef3
Make toPretty() render prettier output
brandonchinn178 Sep 30, 2023
0b22568
Merge branch 'main' into chinn/relative-paths
SimenB Oct 2, 2023
09aed7d
oops
SimenB Oct 2, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
- `[jest-util]` Make sure `isInteractive` works in a browser ([#14552](https://github.com/jestjs/jest/pull/14552))
- `[pretty-format]` [**BREAKING**] Print `ArrayBuffer` and `DataView` correctly ([#14290](https://github.com/facebook/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/facebook/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`
SimenB marked this conversation as resolved.
Show resolved Hide resolved

### Performance

Expand Down
4 changes: 2 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ While code transformation is applied to the linked setup-file, Jest will **not**

```js title="setup.js"
module.exports = async function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPattern);
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);

// Set reference to mongod in order to close the server during teardown.
Expand All @@ -797,7 +797,7 @@ module.exports = async function (globalConfig, projectConfig) {

```js title="teardown.js"
module.exports = async function (globalConfig, projectConfig) {
console.log(globalConfig.testPathPattern);
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);

await globalThis.__MONGOD__.stop();
Expand Down
2 changes: 1 addition & 1 deletion docs/WatchPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ For stability and safety reasons, only part of the global configuration keys can
- [`onlyFailures`](configuration#onlyfailures-boolean)
- [`reporters`](configuration#reporters-arraymodulename--modulename-options)
- [`testNamePattern`](cli#--testnamepatternregex)
- [`testPathPattern`](cli#--testpathpatternregex)
- [`testPathPatterns`](cli#--testpathpatternregex)
- [`updateSnapshot`](cli#--updatesnapshot)
- [`verbose`](configuration#verbose-boolean)

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"printBasicPrototype": false
},
"testFailureExitCode": 1,
"testPathPattern": "",
"testPathPatterns": [],
"testSequencer": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-test-sequencer/build/index.js",
"updateSnapshot": "none",
"useStderr": false,
Expand Down
20 changes: 8 additions & 12 deletions e2e/__tests__/globalSetup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('globalSetup is triggered once before all test suites', () => {
const setupPath = path.join(e2eDir, 'setup.js');
const result = runWithJson(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(result.exitCode).toBe(0);
Expand All @@ -70,7 +70,7 @@ test('jest throws an error when globalSetup does not export a function', () => {
const setupPath = path.resolve(__dirname, '../global-setup/invalidSetup.js');
const {exitCode, stderr} = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(exitCode).toBe(1);
Expand All @@ -83,15 +83,13 @@ test('jest throws an error when globalSetup does not export a function', () => {
test('globalSetup function gets global config object and project config as parameters', () => {
const setupPath = path.resolve(e2eDir, 'setupWithConfig.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--testPathPatterns=pass',
'--cache=true',
]);

expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
});

test('should call globalSetup function of multiple projects', () => {
Expand All @@ -111,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}`,
'--testPathPattern=project-1',
'--testPathPatterns=project-1',
]);

expect(result.exitCode).toBe(0);
Expand Down Expand Up @@ -140,23 +138,21 @@ test('should not call any globalSetup if there are no tests to run', () => {
test('globalSetup works with default export', () => {
const setupPath = path.resolve(e2eDir, 'setupWithDefaultExport.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
'--testPathPatterns=pass',
'--cache=true',
]);

expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
});

test('globalSetup throws with named export', () => {
const setupPath = path.resolve(e2eDir, 'invalidSetupWithNamedExport.js');

const {exitCode, stderr} = runJest(e2eDir, [
`--globalSetup=${setupPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(exitCode).toBe(1);
Expand Down
20 changes: 8 additions & 12 deletions e2e/__tests__/globalTeardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('globalTeardown is triggered once after all test suites', () => {
const teardownPath = path.resolve(e2eDir, 'teardown.js');
const result = runWithJson('global-teardown', [
`--globalTeardown=${teardownPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(result.exitCode).toBe(0);
Expand All @@ -54,7 +54,7 @@ test('jest throws an error when globalTeardown does not export a function', () =
const teardownPath = path.resolve(e2eDir, 'invalidTeardown.js');
const {exitCode, stderr} = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(exitCode).toBe(1);
Expand All @@ -67,15 +67,13 @@ test('jest throws an error when globalTeardown does not export a function', () =
test('globalSetup function gets global config object and project config as parameters', () => {
const teardownPath = path.resolve(e2eDir, 'teardownWithConfig.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
`--testPathPattern=${testPathPattern}`,
'--testPathPatterns=pass',
'--cache=true',
]);

expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
});

test('should call globalTeardown function of multiple projects', () => {
Expand All @@ -95,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}`,
'--testPathPattern=project-1',
'--testPathPatterns=project-1',
]);

expect(result.exitCode).toBe(0);
Expand All @@ -108,15 +106,13 @@ test('should not call a globalTeardown of a project if there are no tests to run
test('globalTeardown works with default export', () => {
const teardownPath = path.resolve(e2eDir, 'teardownWithDefaultExport.js');

const testPathPattern = 'pass';

const result = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
`--testPathPattern=${testPathPattern}`,
'--testPathPatterns=pass',
'--cache=true',
]);

expect(result.stdout).toBe(`${testPathPattern}\ntrue`);
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
});

test('globalTeardown throws with named export', () => {
Expand All @@ -127,7 +123,7 @@ test('globalTeardown throws with named export', () => {

const {exitCode, stderr} = runJest(e2eDir, [
`--globalTeardown=${teardownPath}`,
'--testPathPattern=__tests__',
'--testPathPatterns=__tests__',
]);

expect(exitCode).toBe(1);
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/multiProjectRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ test('"No tests found" message for projects', () => {
'project1',
'project2',
]);
expect(verboseOutput).toContain('Pattern: xyz321 - 0 matches');
expect(verboseOutput).toContain('Pattern: /xyz321/i - 0 matches');
const {stdout} = runJest(DIR, [
'--no-watchman',
'xyz321',
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/noTestsFound.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const DIR = path.resolve(__dirname, '../no-tests-found-test');
describe('No tests are found', () => {
test('fails the test suite in standard situation', () => {
const {exitCode, stdout} = runJest(DIR, [
'--testPathPattern',
'--testPathPatterns',
'/non/existing/path/',
]);

Expand All @@ -26,7 +26,7 @@ describe('No tests are found', () => {

test("doesn't fail the test suite if --passWithNoTests passed", () => {
const {exitCode, stdout} = runJest(DIR, [
'--testPathPattern',
'--testPathPatterns',
'/non/existing/path/',
'--passWithNoTests',
]);
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/onlyChanged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ test('collect test coverage when using onlyChanged', () => {
expect(exitCode).toBe(0);
});

test('onlyChanged in config is overwritten by --all or testPathPattern', () => {
test('onlyChanged in config is overwritten by --all or testPathPatterns', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/file1.test.js': "require('../file1'); test('file1', () => {});",
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/testPathPatternReporterMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ test('prints a message with path pattern at the end', () => {
({stderr} = runJest(DIR, ['a', 'b']));
expect(stderr).toMatch('Ran all test suites matching /a|b/i');

({stderr} = runJest(DIR, ['--testPathPattern', 'a']));
({stderr} = runJest(DIR, ['--testPathPatterns', 'a']));
expect(stderr).toMatch('Ran all test suites matching /a/i');

({stderr} = runJest(DIR, ['--testPathPattern', 'a|b']));
({stderr} = runJest(DIR, ['--testPathPatterns', 'a|b']));
expect(stderr).toMatch('Ran all test suites matching /a|b/i');
});
2 changes: 1 addition & 1 deletion e2e/global-setup/invalidSetupWithNamedExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

function invalidSetupWithNamedExport(jestConfig): void {
console.log(jestConfig.testPathPattern);
console.log(jestConfig.testPathPatterns);
}

export {invalidSetupWithNamedExport};
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.testPathPattern);
console.log(globalConfig.testPathPatterns);
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.testPathPattern);
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);
}
2 changes: 1 addition & 1 deletion e2e/global-teardown/invalidTeardownWithNamedExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

function invalidTeardownWithNamedExport(jestConfig): void {
console.log(jestConfig.testPathPattern);
console.log(jestConfig.testPathPatterns);
}

export {invalidTeardownWithNamedExport};
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.testPathPattern);
console.log(globalConfig.testPathPatterns);
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.testPathPattern);
console.log(globalConfig.testPathPatterns);
console.log(projectConfig.cache);
}
6 changes: 3 additions & 3 deletions packages/jest-cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function check(argv: Config.Argv): true {
}

export const usage =
'Usage: $0 [--config=<pathToConfigFile>] [TestPathPattern]';
'Usage: $0 [--config=<pathToConfigFile>] [TestPathPatterns]';
export const docs = 'Documentation: https://jestjs.io/';

// The default values are all set in jest-config
Expand Down Expand Up @@ -431,7 +431,7 @@ export const options: {[key: string]: Options} = {
},
passWithNoTests: {
description:
'Will not fail if no tests are found (for example while using `--testPathPattern`.)',
'Will not fail if no tests are found (for example while using `--testPathPatterns`.)',
type: 'boolean',
},
preset: {
Expand Down Expand Up @@ -609,7 +609,7 @@ export const options: {[key: string]: Options} = {
string: true,
type: 'array',
},
testPathPattern: {
testPathPatterns: {
description:
'A regexp pattern string that is matched against all tests ' +
'paths before executing the test.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ exports[`testMatch throws if testRegex and testMatch are both specified 1`] = `
<red></color>"
`;

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

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

exports[`testTimeout should throw an error if timeout is a negative number 1`] = `
"<red><bold><bold>● </intensity><bold>Validation Error</intensity>:</color>
Expand Down
Loading