From 1a864f23b48cc1d5d27782f4152f4f5d1abe2873 Mon Sep 17 00:00:00 2001 From: Bruno Farias Date: Sun, 25 Sep 2022 18:58:42 +0100 Subject: [PATCH 1/6] Fixes bug #13222 --- packages/jest-core/src/SearchSource.ts | 4 +-- .../src/__tests__/SearchSource.test.ts | 35 ++++++++++++++++--- packages/jest-core/src/types.ts | 7 +--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/jest-core/src/SearchSource.ts b/packages/jest-core/src/SearchSource.ts index b1c8d31125fd..b7c14833e962 100644 --- a/packages/jest-core/src/SearchSource.ts +++ b/packages/jest-core/src/SearchSource.ts @@ -340,9 +340,7 @@ export default class SearchSource { ); } - const filteredSet = new Set( - filterResult.filtered.map(result => result.test), - ); + const filteredSet = new Set(filterResult.filtered); return { ...searchResult, diff --git a/packages/jest-core/src/__tests__/SearchSource.test.ts b/packages/jest-core/src/__tests__/SearchSource.test.ts index 7e4af257a870..d4a2a05d9e18 100644 --- a/packages/jest-core/src/__tests__/SearchSource.test.ts +++ b/packages/jest-core/src/__tests__/SearchSource.test.ts @@ -11,7 +11,8 @@ import type {Test} from '@jest/test-result'; import type {Config} from '@jest/types'; import {normalize} from 'jest-config'; import Runtime from 'jest-runtime'; -import SearchSource, {SearchResult} from '../SearchSource'; +import SearchSource from '../SearchSource'; +import type {Filter} from '../types'; jest.setTimeout(15000); @@ -103,11 +104,18 @@ describe('SearchSource', () => { }); describe('getTestPaths', () => { - const getTestPaths = async (initialOptions: Config.InitialOptions) => { + const getTestPaths = async ( + initialOptions: Config.InitialOptions, + filter?: Filter, + ) => { const searchSource = await initSearchSource(initialOptions); - const {tests: paths} = await searchSource.getTestPaths({ - testPathPattern: '', - }); + const {tests: paths} = await searchSource.getTestPaths( + { + testPathPattern: '', + }, + null, + filter, + ); return paths.map(({path: p}) => path.relative(rootDir, p)).sort(); }; @@ -288,6 +296,23 @@ describe('SearchSource', () => { path.normalize('__testtests__/test.jsx'), ]); }); + + it('filter tests based on an optional filter method', async () => { + const filterFunction = (testPaths: Array) => + Promise.resolve({ + filtered: testPaths.filter(testPath => testPath.includes('test.jsx')), + }); + const paths = await getTestPaths( + { + id, + rootDir, + }, + filterFunction, + ); + + expect(paths).toHaveLength(1); + expect(paths[0]).toStrictEqual(path.normalize('__testtests__/test.jsx')); + }); }); describe('filterPathsWin32', () => { diff --git a/packages/jest-core/src/types.ts b/packages/jest-core/src/types.ts index 98119f1c6375..84c761d53a1d 100644 --- a/packages/jest-core/src/types.ts +++ b/packages/jest-core/src/types.ts @@ -34,11 +34,6 @@ export type TestPathCasesWithPathPattern = TestPathCases & { testPathPattern: (path: string) => boolean; }; -export type FilterResult = { - test: string; - message: string; -}; - export type Filter = (testPaths: Array) => Promise<{ - filtered: Array; + filtered: Array; }>; From 2f4ee48b266632d89883800a57c5a3cfdcd75cc1 Mon Sep 17 00:00:00 2001 From: Bruno Farias Date: Sun, 25 Sep 2022 19:52:23 +0100 Subject: [PATCH 2/6] Fixed broken integration filter tests --- e2e/filter/my-filter.js | 4 +--- e2e/filter/my-secondary-filter.js | 2 +- e2e/filter/my-setup-filter.js | 4 +--- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/e2e/filter/my-filter.js b/e2e/filter/my-filter.js index 801f5e3bd462..83333a90af40 100644 --- a/e2e/filter/my-filter.js +++ b/e2e/filter/my-filter.js @@ -11,9 +11,7 @@ module.exports = function (tests) { return new Promise(resolve => { setTimeout(() => { resolve({ - filtered: tests - .filter(t => t.indexOf('foo') !== -1) - .map(test => ({message: 'some message', test})), + filtered: tests.filter(t => t.indexOf('foo') !== -1), }); }, 100); }); diff --git a/e2e/filter/my-secondary-filter.js b/e2e/filter/my-secondary-filter.js index f0b23d0579df..e8df9462aa29 100644 --- a/e2e/filter/my-secondary-filter.js +++ b/e2e/filter/my-secondary-filter.js @@ -9,6 +9,6 @@ module.exports = function (tests) { return { - filtered: tests.filter(t => t.indexOf('foo') !== -1).map(test => ({test})), + filtered: tests.filter(t => t.indexOf('foo') !== -1), }; }; diff --git a/e2e/filter/my-setup-filter.js b/e2e/filter/my-setup-filter.js index 9fc6f3b12d8c..171e3473cbd5 100644 --- a/e2e/filter/my-setup-filter.js +++ b/e2e/filter/my-setup-filter.js @@ -13,9 +13,7 @@ const setupData = { module.exports = function (tests) { return { - filtered: tests - .filter(t => t.indexOf(setupData.filterText) !== -1) - .map(test => ({test})), + filtered: tests.filter(t => t.indexOf(setupData.filterText) !== -1), }; }; From a75ba48b6cd19d62df1519020f1f63b5b68d37cd Mon Sep 17 00:00:00 2001 From: Bruno Farias Date: Thu, 21 Sep 2023 13:05:05 +0100 Subject: [PATCH 3/6] Updated --filter documentation and fixed spacing issue as noticed by linter --- CHANGELOG.md | 1 + docs/CLI.md | 9 +++++---- packages/jest-core/src/__tests__/SearchSource.test.ts | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 525ddd793b52..7b1f06f76719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544)) - `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) +- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319)) ### Fixes diff --git a/docs/CLI.md b/docs/CLI.md index 033fa6a83069..32eb7dfda539 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -194,13 +194,14 @@ Alias: `-e`. Use this flag to show full diffs and errors instead of a patch. ### `--filter=` -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. +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 }` containing the tests that should be run by Jest. Especially useful when used in conjunction with a testing infrastructure to filter known broken tests. ```js title="my-filter.js" +// 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): +const filteringFunction = (testPath) => testPath.endsWith('.spec.js'); + module.exports = testPaths => { - const allowedPaths = testPaths - .filter(filteringFunction) - .map(test => ({test})); // [{ test: "path1.spec.js" }, { test: "path2.spec.js" }, etc] + const allowedPaths = testPaths.filter(filteringFunction); // ["path1.spec.js", "path2.spec.js", etc] return { filtered: allowedPaths, diff --git a/packages/jest-core/src/__tests__/SearchSource.test.ts b/packages/jest-core/src/__tests__/SearchSource.test.ts index 4559aa116d62..80ed03a6ef3f 100644 --- a/packages/jest-core/src/__tests__/SearchSource.test.ts +++ b/packages/jest-core/src/__tests__/SearchSource.test.ts @@ -113,7 +113,7 @@ describe('SearchSource', () => { const {searchSource, config} = await initSearchSource(initialOptions); const {tests: paths} = await searchSource.getTestPaths( { - ...config, + ...config, testPathPattern: '', }, null, From c45193cb9d55d41159829f5c249c4e2a5d5dec15 Mon Sep 17 00:00:00 2001 From: Bruno Farias Date: Thu, 21 Sep 2023 13:35:49 +0100 Subject: [PATCH 4/6] Removed unnecessary parenthesis --- docs/CLI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CLI.md b/docs/CLI.md index 32eb7dfda539..f6d276ef6021 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -198,7 +198,7 @@ Path to a module exporting a filtering function. This asynchronous function rece ```js title="my-filter.js" // 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): -const filteringFunction = (testPath) => testPath.endsWith('.spec.js'); +const filteringFunction = testPath => testPath.endsWith('.spec.js'); module.exports = testPaths => { const allowedPaths = testPaths.filter(filteringFunction); // ["path1.spec.js", "path2.spec.js", etc] From 6b8741b69428e4681a50bee92e7aca93368e8a0e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 16 Nov 2023 10:35:20 +0100 Subject: [PATCH 5/6] move changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b75c89b0b3d..dd908cf69363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584)) - `[@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)) - `[@jest/core]` Add `perfStats` to surface test setup overhead ([#14622](https://github.com/jestjs/jest/pull/14622)) +- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319)) - `[@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)) - `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825)) - `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544)) @@ -17,7 +18,6 @@ - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) - `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566)) - `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470)) -- `[@jest/core]` [**BREAKING**] Changed `--filter` to accept an object with shape `{ filtered: Array }` to match [documentation](https://jestjs.io/docs/cli#--filterfile) ([#13319](https://github.com/jestjs/jest/pull/13319)) ### Fixes From e32d042a5163728a0fc9c10645497f135daa0677 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 16 Nov 2023 11:19:44 +0100 Subject: [PATCH 6/6] lint --- packages/jest-core/src/__tests__/SearchSource.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-core/src/__tests__/SearchSource.test.ts b/packages/jest-core/src/__tests__/SearchSource.test.ts index ec635c471436..1ec593bbf37d 100644 --- a/packages/jest-core/src/__tests__/SearchSource.test.ts +++ b/packages/jest-core/src/__tests__/SearchSource.test.ts @@ -115,7 +115,7 @@ describe('SearchSource', () => { { ...config, ...initialOptions, - testPathPatterns: [], + testPathPatterns: [], }, null, filter,