Skip to content

Commit 3e86cf3

Browse files
feat: --filter module returns array of strings, not objects (#13319)
1 parent c04d13d commit 3e86cf3

File tree

8 files changed

+42
-26
lines changed

8 files changed

+42
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
88
- `[@jest/core]` [**BREAKING**] Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14543](https://github.com/jestjs/jest/pull/14543))
99
- `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622))
10+
- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array<string> }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319))
1011
- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
1112
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
1213
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))

docs/CLI.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,14 @@ Alias: `-e`. Use this flag to show full diffs and errors instead of a patch.
194194

195195
### `--filter=<file>`
196196

197-
Path to a module exporting a filtering function. This asynchronous function receives a list of test paths which can be manipulated to exclude tests from running by returning an object with shape `{ filtered: Array<{ test: string }> }`. Especially useful when used in conjunction with a testing infrastructure to filter known broken tests, e.g.
197+
Path to a module exporting a filtering function. This asynchronous function receives a list of test paths which can be manipulated to exclude tests from running and must return an object with shape `{ filtered: Array<string> }` containing the tests that should be run by Jest. Especially useful when used in conjunction with a testing infrastructure to filter known broken tests.
198198

199199
```js title="my-filter.js"
200+
// This filter when applied will only run tests ending in .spec.js (not the best way to do it, but it's just an example):
201+
const filteringFunction = testPath => testPath.endsWith('.spec.js');
202+
200203
module.exports = testPaths => {
201-
const allowedPaths = testPaths
202-
.filter(filteringFunction)
203-
.map(test => ({test})); // [{ test: "path1.spec.js" }, { test: "path2.spec.js" }, etc]
204+
const allowedPaths = testPaths.filter(filteringFunction); // ["path1.spec.js", "path2.spec.js", etc]
204205

205206
return {
206207
filtered: allowedPaths,

e2e/filter/my-filter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ module.exports = function (tests) {
1111
return new Promise(resolve => {
1212
setTimeout(() => {
1313
resolve({
14-
filtered: tests
15-
.filter(t => t.includes('foo'))
16-
.map(test => ({message: 'some message', test})),
14+
filtered: tests.filter(t => t.includes('foo')),
1715
});
1816
}, 100);
1917
});

e2e/filter/my-secondary-filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
module.exports = function (tests) {
1111
return {
12-
filtered: tests.filter(t => t.includes('foo')).map(test => ({test})),
12+
filtered: tests.filter(t => t.includes('foo')),
1313
};
1414
};

e2e/filter/my-setup-filter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const setupData = {
1313

1414
module.exports = function (tests) {
1515
return {
16-
filtered: tests
17-
.filter(t => t.includes(setupData.filterText))
18-
.map(test => ({test})),
16+
filtered: tests.filter(t => t.includes(setupData.filterText)),
1917
};
2018
};
2119

packages/jest-core/src/SearchSource.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ export default class SearchSource {
339339
);
340340
}
341341

342-
const filteredSet = new Set(
343-
filterResult.filtered.map(result => result.test),
344-
);
342+
const filteredSet = new Set(filterResult.filtered);
345343

346344
return {
347345
...searchResult,

packages/jest-core/src/__tests__/SearchSource.test.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {Config} from '@jest/types';
1212
import {normalize} from 'jest-config';
1313
import Runtime from 'jest-runtime';
1414
import SearchSource from '../SearchSource';
15+
import type {Filter} from '../types';
1516

1617
jest.setTimeout(15_000);
1718

@@ -105,13 +106,20 @@ describe('SearchSource', () => {
105106
});
106107

107108
describe('getTestPaths', () => {
108-
const getTestPaths = async (initialOptions: Config.InitialOptions) => {
109+
const getTestPaths = async (
110+
initialOptions: Config.InitialOptions,
111+
filter?: Filter,
112+
) => {
109113
const {searchSource, config} = await initSearchSource(initialOptions);
110-
const {tests: paths} = await searchSource.getTestPaths({
111-
...config,
112-
...initialOptions,
113-
testPathPatterns: [],
114-
});
114+
const {tests: paths} = await searchSource.getTestPaths(
115+
{
116+
...config,
117+
...initialOptions,
118+
testPathPatterns: [],
119+
},
120+
null,
121+
filter,
122+
);
115123
return paths.map(({path: p}) => path.relative(rootDir, p)).sort();
116124
};
117125

@@ -292,6 +300,23 @@ describe('SearchSource', () => {
292300
path.normalize('__testtests__/test.jsx'),
293301
]);
294302
});
303+
304+
it('filter tests based on an optional filter method', async () => {
305+
const filterFunction = (testPaths: Array<string>) =>
306+
Promise.resolve({
307+
filtered: testPaths.filter(testPath => testPath.includes('test.jsx')),
308+
});
309+
const paths = await getTestPaths(
310+
{
311+
id,
312+
rootDir,
313+
},
314+
filterFunction,
315+
);
316+
317+
expect(paths).toHaveLength(1);
318+
expect(paths[0]).toStrictEqual(path.normalize('__testtests__/test.jsx'));
319+
});
295320
});
296321

297322
describe('filterPathsWin32', () => {

packages/jest-core/src/types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ export type TestPathCasesWithPathPattern = TestPathCases & {
3434
testPathPatterns: (path: string) => boolean;
3535
};
3636

37-
export type FilterResult = {
38-
test: string;
39-
message: string;
40-
};
41-
4237
export type Filter = (testPaths: Array<string>) => Promise<{
43-
filtered: Array<FilterResult>;
38+
filtered: Array<string>;
4439
}>;

0 commit comments

Comments
 (0)