Musket Musket - 1 month ago 19x
Node.js Question

How do I test a single file using Jest?

I am able to test multiple files using Jest, but I cannot figure out how to test a single file.

I have:

  • Run
    npm install jest-cli --save-dev

  • Updated
    : `{ ... "scripts": { "test": "jest" } ... }

  • Written a number of tests.

npm test
works as expected (currently it runs 14 tests).

How do I test a single file, e.g. test

I have tried running
npm test app/foo/__tests__/bar.spec.js
(from project root), but I get the following error:

npm ERR! Error: ENOENT, open '<path to project>/node_modules/app/foo/__tests__/bar.spec.js/package.json'



If npm test works, it means jest is installed globally. In order to run a specific test, you'll need to use the jest command- npm test will not work.

So you can run jest directly with jest bar.spec.js

Note: You don't have to enter the full path of your test: the argument becomes a regexp, thus it only needs to be part of the full path.

Note 2.0: If you have used npm install -g jest to install jest globally, change it as npm install -g jest-cli