Set up with Jest in Create React App
This guide will use specific versions NPM packages. Please double-check that you installed the same versions to avoid installation issues!
- Install Create React App
$ npm install -g [email protected]
- Create a new React application
$ create-react-app my-app
$ cd my-app
- Install
react-page-object
,enzyme
, andreact-test-renderer
$ npm i -D react-page-object [email protected] [email protected]
- Modify the contents of
src/App.test.js
to be:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import Page from 'react-page-object';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
describe('Passing test', () => {
let page;
beforeEach(() => {
page = new Page(<App />)
});
afterEach(() => {
page.destroy();
});
it('should pass', () => {
expect(page.content()).toMatch(/Welcome to React/)
});
})
At this point, the set up is complete! You can run Jest as before with:
$ npm test