In accordance with the React devs suggestion, we're going to use Jest to run our tests. However, if you want to try a different test framework, there's nothing stopping you! Don't get confused between React and Jest -they're entirely different and independent. If you like Jest, it could be your go to all purpose test suite..
Jest doesn't play well with version 0.12 of Node. If you experience errors (e.g. Segmentation Fault), you may need to install npm packages n
or nvm
to roll back your version of node to 0.10.x. Version 0.10.33 seems to work well.
If using an older version of node, you may get an error message requiring you to use --harmony
command line flag when running your tests. You can get around this by installing npm harmonize
Instructions here.
There are plenty of complications to come, but here are the basics of getting a Jest test suite up and running.
Create a __test__
directory in your project directory.
Create a package.json (e.g. use npm init
).
Install jest-cli and add it to your package.json:
npm install jest-cli --save-dev
In your package.json, set the 'test' script to 'jest':
{
...json
"scripts": {
"test": "jest"
}
...
}
Run your tests with npm test
. Jest will find and run all tests in your test dir.
// sum.js
function sum(value1, value2) {
return value1 + value2;
}
module.exports = sum;
Create a file for your tests in your __tests__
dir.
Require the file you're testing. Jest different syntax for this, to the usual node 'require'. Jest mocks functions by default, but we don't want it to mock the code we're actually testing. So we write:
jest.dontMock('../fileToTest.js');
// __tests__/sum-test.js
jest.dontMock('../sum');
describe('sum', function() {
it('adds 1 + 2 to equal 3', function() {
var sum = require('../sum');
expect(sum(1, 2)).toBe(3);
});
});
A typical Jasmine test takes this form:
describe("A test suite name", function() {
it("A description of your expectation", function() {
expect(true).toBe(true);
});
});
describe() opens a new test block. The first parameter is a string describing the suite of tests. It's for your benefit, it doesn't e.g. need to match any function names. The second parameter is a callback which will hold your tests.
it() starts a new test. The first parameter is a string describing your expectation for this test. The second parameter is a callback which will hold this test (or 'expectation').
expect() takes the function call you are testing as an argument.
toBe() is a matcher. *It takes your expected results as an argument.
Matchers. Jasmine has a range of these. Consult the docs, but key examples are:
At last!
var React = require('react/addons'),
TestUtils = React.addons.TestUtils;
preprocessor.js
containing the following code: var ReactTools = require('react-tools');
module.exports = {
process: function(src) {
return ReactTools.transform(src);
}
};
support
dir. "jest": {
"scriptPreprocessor": "<rootDir>/support/preprocessor.js",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react"
]
}