Search code examples
node.jsunit-testingmocha.js

Run a single test file using Node js Mocha


All my tests are under test folder and when I give npm test all the tests are getting executed. Now I am trying to run my test scripts by file or by it's describe. I have the following in my package.json

"scripts": {
  "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000"}

When I give npm test --grep 'filename / describe / it' it is not picking up the given input instead I'm getting the following warning Warning: Cannot find any files matching pattern "given filename / describe /it" even though given input matches.

My filename pattern is filename.test.js and the folder structure is like test\***\filename.test.js

Can anyone guide me on what I'm missing on this.


Solution

  • You need to include the grep option within the npm command:

    "scripts": {
      "test": "set NODE_ENV=test&& nyc --reporter=html --reporter=text mocha 'test/**/*.js' --exit --timeout 7000 --grep"
    }
    

    Then call it with:

    npm run test <name of test>