Run only one test file with Jest

Published June 10, 2021

If you'd like to run only one test file with Jest to see the result of that file's tests, here is how you can do that.

Make sure your package.json file contains the following.

"scripts": {
  "test": "jest --runInBand --forceExit",
}

Then, run the following command.

npm test -- user.service.spec.ts

The -- marks the end of options. Anything entered after the -- is passed to the command being run, which is jest in this case. Thus, the file user.service.spec.ts is passed to the jest command to run that file's tests.