3

I have Jest as my Test Runner. I also have husky in my commit hook. I want to run on every commit, the tests that are affected by my changes and related ones.

I see in the documentation for Jest the following options, but I am not making progress on making them work:

--findRelatedTests -> To find the related test to my changes 
--testPathPattern -> To execute tests only on specific folders

Do you have a documentation or any examples that could be helpful. Or explain to me how I could achieve that.? Thanks!!

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • 3
    What exactly was a confusion with findRelatedTests? Docs are quite precise on that, you need to provide a list of changed files to it. I expect it something like https://github.com/typicode/husky/issues/412 , i.e. `jest --findRelatedTest $(git status --porcelain | grep M | awk '{print $2}')` , won't work If you're targeting windows. Did you check Jest `onlyChanged`? It seems to be the case. – Estus Flask Jul 13 '20 at 16:59

1 Answers1

1

Looking into packages/jest-cli/src/cli/args.ts we can find:

throw new Error(
  'The --findRelatedTests option requires file paths to be specified.\n' +
    'Example usage: jest --findRelatedTests ./src/source.js ' +
    './src/index.js.',
);

I know it may not work under some circumstances(it does work in one my project and does not work in another, config is pretty close but versions are different), so keep in mind even if it does not work - it still may be not because misconfiguration or alike.

testPathPattern is often specified through package.json's jest section -> testMatch like:

"testMatch": ["**/?(*.)+(spec).js"]
skyboyer
  • 22,209
  • 7
  • 57
  • 64