Jest

Run:

1
yarn add --dev react-test-renderer

Setup without Create React App

If you have an existing application you’ll need to install a few packages to make everything work well together. We are using the babel-jest package and the react babel preset to transform our code inside of the test environment.

1
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react react-test-renderer

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// package.json
"dependencies": {
"react": "<current-version>",
"react-dom": "<current-version>"
},
"devDependencies": {
"@babel/preset-env": "<current-version>",
"@babel/preset-react": "<current-version>",
"babel-jest": "<current-version>",
"jest": "<current-version>",
"react-test-renderer": "<current-version>"
},
"scripts": {
"test": "jest"
}

1
2
3
4
  // babel.config.js
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
};

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Link.react.js
import React from 'react';

const STATUS = {
HOVERED: 'hovered',
NORMAL: 'normal',
};

export default class Link extends React.Component {
constructor(props) {
super(props);

this._onMouseEnter = this._onMouseEnter.bind(this);
this._onMouseLeave = this._onMouseLeave.bind(this);

this.state = {
class: STATUS.NORMAL,
};
}

_onMouseEnter() {
this.setState({class: STATUS.HOVERED});
}

_onMouseLeave() {
this.setState({class: STATUS.NORMAL});
}

render() {
return (
<a
className={this.state.class}
href={this.props.page || '#'}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
>
{this.props.children}
</a>
);
}
}

example: