Search code examples
jestjsgulp-jest

Run single test with jest.runCLI()


I want to use jest.runCLI() in gulp for executing only a changed test.

How can I do this?

How can I run just one test with jest.runCLI()?


Solution

  • Jest has a CLI option to run only changed files (based on the git repository status). The command line jest --onlyChanged becomes programmatically:

    jest.runCLI({'onlyChanged': true}, __dirname, function (success) {...});
    

    Using the terminal, jest uses non-hyphenated options to specified filenames:

    • jest test1 runs only test1.js
    • jest test1 test2 runs test1.js and test2.js

    jest-cli uses optimist to parse options and non-hyphenated option are mapped to the underscore option (_):

    • jest.runCLI({'_': ['test1']}, __dirname, function (success) {...}); runs only test1.js
    • jest.runCLI({'_': ['test1', 'test2']}, __dirname, function (success) {...}); runs test1.js and test2.js