React & Modern Frontend (445 Qs)•Part 4: React Testing & Jest (95 - 100)
What are the advantages of Jest over Jasmine?
There are couple of advantages compared to Jasmine:
- Automatically finds tests to execute in your source code.
- Automatically mocks dependencies when running your tests.
- Allows you to test asynchronous code synchronously.
- Runs your tests with a fake DOM implementation (via
jsdom) so that your tests can be run on the command line. - Runs tests in parallel processes so that they finish sooner.
Practical Code Example
javascript
// Advantages of Jest: Zero config, parallelized test runs, built-in code coverage, and snapshot testing:
// jest --coverage
test('matches snapshot', () => {
const tree = renderer.create(<Button label="Save" />).toJSON();
expect(tree).toMatchSnapshot();
});