Jest - Find related tests and run them on `pre-commit` Hook

Viewed 2233

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!!

1 Answers

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"]
Related