jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: --filter argument inconsistent with documentation

Open brunocabral88 opened this issue 2 years ago • 1 comments

Version

v29.0.2

Steps to reproduce

  1. Install the latest version of Jest in a repository with existing test files
  2. Confirm Jest runs all your test files properly by running jest without arguments
  3. Create a file called filter.js in the current folder and paste the below contents
module.exports = testPaths => {
  return {
    filtered: testPaths,
  };
};
  1. Run jest --filter=<absolute-path>/filter.js

Expected behavior

I would expect Jest to run all the tests, considering I am not filtering any tests from the given input

Actual behavior

Jest outputs Pattern: - 0 matches

Additional context

This is not consistent with what the documentation (that can be found here) suggests.

From the docs: 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 the "filtered" property. Especially useful when used in conjunction with a testing infrastructure to filter known broken, e.g.

However, in the SearchSource.getTestPaths, we can see that it looks for a property called test in every item returned from the filter.

A workaround I did in my environment was to have it return the expected property, like below:

module.exports = (testPaths) => {
  ... code omitted ...
  return {
    filtered: testsToExecute.map((test) => ({ test })),
  };
}

We should probably either update the code so it only expects an array of string as the documentation suggests, or update the documentation to specify it requires the filtered property to have an array of shape { test: string}

Environment

System:
	OS: Windows 11 Home
	CPU: (8) x86-64
Binaries:
	Node: 16.14.0
	Yarn: 1.22.15
	npm: 8.3.1
npmPackages:
	jest: 29.0.2 (also tested on 26.6.3)

brunocabral88 avatar Sep 07 '22 14:09 brunocabral88

As mentioned in #12260, I believe the fix is fairly simple. Either update the documentation to specify that the return type is

type T = {
    filtered: { test: string }[];
};

or change this line SearchSource.js to

const filteredSet = new Set(filterResult.filtered);

I will say that the latter seems more clear as the current implementation seems redundant.

jfw225 avatar Sep 20 '22 14:09 jfw225

Thanks @jfw225, I think it makes more sense to just return an array of strings in the shape of { filtered: Array<string> }, considering the method accepts an array of strings too. I just created a PR for it.

brunocabral88 avatar Sep 25 '22 18:09 brunocabral88

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Oct 30 '22 00:10 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Oct 21 '23 00:10 github-actions[bot]

https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.3

SimenB avatar Feb 20 '24 11:02 SimenB