question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Documentation]: CLI --filter option might no be clear enough / Filter {filterPath} did not return a valid test list

See original GitHub issue

Version

27.4.5

Steps to reproduce

Steps : (1) Go to the documentation about filter file (2) Create a working project with a valid jest config (3) Create a filter file as stated in the doc to be used with bash --filter=<filename> (4) Try a few functions (5) Receive “Filter {filterPath} did not return a valid test list”

Expected behavior

I expected the documentation to provide what I was supposed to write in the file, that is :

“[a] Path to a module exporting a filtering function.”

One could expect a function of the type

function filteringFunction(files:string[]):string[]{
  return files.filter( /** logic **/ ) 
}

Or even :

function isFileValid(file:string):boolean {
  return applyLogic(file)
}

Actual behavior

But, because the code is the following :


if (filter) { 
      const tests = searchResult.tests;
      const filterResult = await filter(tests.map(test => test.path));

      console.log({ filterResult })

      if (!Array.isArray(filterResult.filtered)) {
        console.log(filterResult.filtered)
        throw new Error(
          `Filter ${filterPath} did not return a valid test list`
        );
 }

and also


 const filteredSet = new Set(
        filterResult.filtered.map(result => result.test),
      );

      return {
        ...searchResult,
        tests: tests.filter(test => filteredSet.has(test.path)),
      };

I got it to work by using the very undocumented:


module.exports = function filter(testPaths) {
  const filtered = testPaths
    .filter((testPath) => isFiltered(testPath))
    .map((testPath) => ({ test: testPath }));

  return {
    filtered,
  };
};

I also discovered it was AN ASYNC function 😃

Additional context

I would suggest first to maybe update the documentation with something of the sort:

--filter=<file>

Path to a module exporting a filtering function. This asynchronous function receives a list of 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.

Example:


// my-filter.js
module.exports = (testPaths) => {
  const allowedPaths = filterPaths(testPaths) // ["path1.spec.js", "path2.spec.js", etc]
  return {
      filtered : allowedPaths,
   }
}

And (but I don’t know if it’s possible), to edit the set creation

const filteredSet = new Set(
   ❌ filterResult.filtered.map(result => result.test) 
   ✅ filterResult.filtered
);

Environment

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
  npmPackages:
    jest: ^27.4.5 => 27.4.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gramscocommented, May 3, 2022

@SimenB Done! Waiting for the CLA.

1reaction
mrazauskascommented, Apr 22, 2022

Just to leave a note. Jest’s end-to-end tests is a great usage reference too: https://github.com/facebook/jest/tree/main/e2e/filter

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtering AWS CLI output - AWS Command Line Interface
Server-side filtering in the AWS CLI is provided by the AWS service API. The AWS service only returns the records in the HTTP...
Read more >
Filter search results | Elasticsearch Guide [8.5] | Elastic
When you use the post_filter parameter to filter search results, the search hits are filtered after the aggregations are calculated. A post filter...
Read more >
tshark(1) Manual Page - Wireshark
That library supports specifying a filter expression; packets that don't match that filter are discarded. The -f option is used to specify a...
Read more >
Flatpak Command Reference
Below these locations, there is a local repository in the repo/ subdirectory and installed runtimes and applications are in the corresponding runtime/ and...
Read more >
bladeRF-cli - command line interface and test utility
A non-zero return status will be returned if no devices are available. ... Commands are executed in the following order: Command line options,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found