A repository containing a Jest Example for React
“Jesters do oft prove prophets.” ― William Shakespeare, King Lear
Currently, Jest 0.4 works on Node, while Jest 0.5 works on Io.js. When Node 4.0 comes out, this will no longer be an issue.
Now that Node v4.0 has been released, use Node 4.0+ with Jest 0.5+
- You must have a "jest" section in your
package.json
orjest
will quietly fail. TheunmockedModulePathPatterns
property is really important asdontMock
does not work very well for modules. Here's an example.
"jest": {
"unmockedModulePathPatterns": [
"react",
"chai"
]
}
- Jest 0.5x only works with io.js. Jest 0.4x works (?) with node.js (and possibly io.js as well, but if you're running Io, it makes sense to use
0.5x
for Jest - Jest will automatically mock anything you
require
, which can lead to very confusing results. require statements often don't mean what you think they do inJest
(https://facebook.github.io/jest/docs/automatic-mocking.html)
npm install --save jest jest-cli
npm install --g jest-cli
npm init
- Add
jest
section to package.json - Create
__tests__/basic-test.js
(Code inside this file will run when you calljest
from the command line)
There is a property in the package.json
Jest configuration specifically for this called scriptPreprocessor
. Here is a generic preprocessor for JSX from the docs:
var ReactTools = require('react-tools');
module.exports = {
process: function(src) {
return ReactTools.transform(src);
}
};
And an example configuration, also from the docs:
"jest": {
"scriptPreprocessor": "./preprocessor.js",
The "default-mocking" framework (is that why they call it Jest?)
A tool for using Jest from the command line. Install it globally unless you're using a tool like Gulp to automate Jesting.
A library often used with Jest. This is a bit confusing because, though they are often thought of together and made by Facebook, Jest can be used to test other things. Jest is easiest understood seperate from React (Just as Karma is more easily understood if seperated from Angular)
An internet company which maintains Jest, as well as React.
A tool that comes with React that lets you easily generate virtual instances of components for testing. Allows for convenient functions like findRenderedDOMComponentWithTag
that are somewhat reminiscent of Protractor.