Skip to content

Fixes bug #13222 #13319

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

Merged
merged 8 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 1 addition & 3 deletions e2e/filter/my-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/filter/my-secondary-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
};
4 changes: 1 addition & 3 deletions e2e/filter/my-setup-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
};

Expand Down
4 changes: 1 addition & 3 deletions packages/jest-core/src/SearchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 30 additions & 5 deletions packages/jest-core/src/__tests__/SearchSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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<string>) =>
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', () => {
Expand Down
7 changes: 1 addition & 6 deletions packages/jest-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ export type TestPathCasesWithPathPattern = TestPathCases & {
testPathPattern: (path: string) => boolean;
};

export type FilterResult = {
test: string;
message: string;
};

export type Filter = (testPaths: Array<string>) => Promise<{
filtered: Array<FilterResult>;
filtered: Array<string>;
}>;