Running Jest with npx and npm
Understand how to install Jest as a development dependency and run it using npx and npm scripts. This lesson teaches you how to manage Jest versions, use package lock files, execute tests locally, and ensure consistent test environments for React projects.
We'll cover the following...
We'll cover the following...
Adding Jest as a dependency
Use npm to install the jest package:
$ npm install --save-dev jest@23.6.0 # 1
...
npm created a lockfile as package-lock.json. You should commit this file. # 2
+ jest@23.6.0
added 538 packages from 269 contributors in 24.446s # 3
...
- The
--save-devflag tellsnpm, “I want to use thejestpackage for development only. My project doesn’t need it at runtime.” Thejest@23.6.0means “I want version23.6.0of thejestpackage.”- For reasons that are too involved to go into here, the information in
package.jsonisn’t enough for two machines to be guaranteed to get the samenode_modules, even if you specify exact versions for all dependencies. That’s whynpmcreates the lockfile. If you’re curious about the details, check out thenpmdocs.- The “538 packages” figure is striking, but it’s propper for Node packages: You depend on one package, which depends on several other packages, which each, in turn, depend on several more, and so on.
npm