- Create an empty folder
- Go into the folder
- Init package
- Install dependencies
- Add babel.config.js
- Add src and test folders
- Add button.js in src folder
- Add button.spec.js in test folder
- Run the tests
- step-by-step-gif
mkdir example-react-aw
cd example-react-aw
npm init
Just press enter until you reach test command
fill in aw
With npm
npm i -D @after-work.js/aw @babel/core @babel/preset-env babel-plugin-istanbul @babel/plugin-transform-react-jsx react react-test-renderer
or yarn
yarn add -D @after-work.js/aw @babel/core @babel/preset-env babel-plugin-istanbul @babel/plugin-transform-react-jsx react react-test-renderer
babel.config.js
module.exports = {
presets: [
['@babel/preset-env', {
targets: { node: 'current' },
}],
],
plugins: [
'@babel/plugin-transform-react-jsx',
],
}
mkdir src
mkdir test
src/button.js
import React, { Component } from 'react';
class Button extends Component {
render() {
return (
<button>{this.props.children}</button>
);
}
}
export default Button;
test/button.spec.js
import React from 'react';
import renderer from 'react-test-renderer';
import Button from '../src/button';
describe('button', () => {
it('renders correctly', () => {
const tree = renderer.create(<Button>Text1</Button>).toJSON();
expect(tree).toMatchSnapshot();
});
});
npm test