unit-testingjestjsjest-puppeteer

Testing - How to split up pre-commit unit tests and CI end-to-end tests


Scenario

I'm working on an app that has fast unit/functional jest tests along with slower end-to-end jest-puppeteer tests. I would like to split those up so that I can run the faster tests as part of a git pre-commit hook and leave the end-to-end tests to be run on CI after the code is eventually pushed to origin.

Question

How can I define specific tests to run at pre-commit? Specifically via regex similar to jest moduleNameMapper eg <rootDir>/__tests__/[a-z]+\.unit\.test\.js


in package.json add test:pre which uses bash find . -regex with bash for do to run desired "pre commit" tests


Solution

  • I've added

    "test:pre": "PRE=1 npm test -- test/pre-*test.js" 
    # everything after -- is a kind of regex filter for matching file names
    

    to my package.json scripts and in my jest-puppeteer global-setup.js I'm using

    if(+process.env.PRE) return;
    

    before all the puppeteer extras are started. So now I can

    $ npm run test:pre
    

    and violá