March 25, 2018

Running Jest using Gulp with code coverage

Jest is a unit test framework that is mainly used for reactjs. Jest is widely used by facebook in their projects.

Add below configuration in gulpfile.js for running Jest unit test cases through gulp


  const gulp = require('gulp');
  const jestcli = require('jest-cli');

  gulp.task('test', () => {  
   jestcli.runCLI({ config: { rootDir: 'test/unit/' } }, '.', (done) => {
    done();
   });
  });

Using command: gulp test, will execute the test cases written under directory 'test/unit'. To have code coverage, we can configure it in package.json file as below:

  "jest": {
    "collectCoverage": true,
    "coverageDirectory": "./test/unit-test-coverage"
  }

Add above config in package.json file. for defining custom directory to create the report, mention the directory in coverageDirectory key, otherwise it will generate at root directory.

1 comment: