Skip to content

Commit c9810b1

Browse files
committed
Added Jest and Enzyme with a sample test
1 parent 71b9d53 commit c9810b1

File tree

7 files changed

+2411
-66
lines changed

7 files changed

+2411
-66
lines changed

Diff for: __tests__/config/test.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
4+
configure({ adapter: new Adapter() });

Diff for: __tests__/index.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-disable */
2+
3+
import React from 'react';
4+
import ReactDOM from 'react-dom';
5+
import { shallow } from 'enzyme';
6+
7+
import Layout from '../src/Layout';
8+
9+
it('renders without crashing', () => {
10+
shallow(<Layout />);
11+
});

Diff for: index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import Layout from './src/Layout';
4+
5+
const App = () => <Layout />;
6+
7+
ReactDOM.render(<App />, document.getElementById('root'));

Diff for: package.json

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,42 @@
1212
"start": "webpack-dev-server --mode development",
1313
"format": "prettier --write \"src/**/*.js\"",
1414
"stats": "webpack-bundle-analyzer stats/stats.json",
15-
"build": "webpack --mode production"
15+
"build": "webpack --mode production",
16+
"test": "jest",
17+
"test:watch": "jest --watch",
18+
"test:coverage": "jest --coverage"
19+
},
20+
"jest": {
21+
"setupFiles": [
22+
"<rootDir>__tests__/config/test.config.js"
23+
],
24+
"snapshotSerializers": [
25+
"enzyme-to-json/serializer"
26+
],
27+
"testPathIgnorePatterns": [
28+
"<rootDir>/__tests__/config/"
29+
]
1630
},
1731
"devDependencies": {
1832
"@babel/core": "^7.2.2",
1933
"@babel/preset-env": "^7.3.1",
2034
"@babel/preset-react": "^7.0.0",
2135
"babel-eslint": "^10.0.1",
36+
"babel-jest": "^24.0.0",
2237
"babel-loader": "^8.0.5",
38+
"babel-preset-env": "^1.7.0",
39+
"babel-preset-react": "^6.24.1",
40+
"enzyme": "^3.8.0",
41+
"enzyme-adapter-react-16": "^1.8.0",
42+
"enzyme-to-json": "^3.3.5",
2343
"eslint": "^5.12.1",
2444
"eslint-config-react": "^1.1.7",
2545
"eslint-loader": "^2.1.1",
2646
"eslint-plugin-react": "^7.12.4",
2747
"html-webpack-plugin": "^3.2.0",
48+
"jest": "^24.0.0",
2849
"prettier": "1.16.2",
50+
"react-test-renderer": "^16.7.0",
2951
"webpack": "^4.29.0",
3052
"webpack-bundle-analyzer": "^3.0.3",
3153
"webpack-cli": "^3.2.1",

Diff for: src/index.js renamed to src/Layout.jsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
32
import styled from 'styled-components';
43

54
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
@@ -57,7 +56,7 @@ module.exports = Pattern;
5756
5857
`;
5958

60-
class Welcome extends React.Component {
59+
class Layout extends React.Component {
6160
render() {
6261
return (
6362
<React.Fragment>
@@ -74,4 +73,4 @@ class Welcome extends React.Component {
7473
}
7574
}
7675

77-
ReactDOM.render(<Welcome />, document.getElementById('root'));
76+
export default Layout;

Diff for: webpack.config.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
33
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
44

55
module.exports = {
6+
mode: 'development',
67
devtool: 'inline-source-map',
7-
entry: './src/index.js',
8+
entry: './index.js',
89
output: {
910
path: path.join(__dirname, '/build'),
1011
publicPath: '/',
11-
filename: 'bundle.js'
12+
filename: '[name].js'
1213
},
1314
optimization: {
1415
splitChunks: {
@@ -21,15 +22,15 @@ module.exports = {
2122
module: {
2223
rules: [
2324
{
24-
test: /\.(js|jsx)$/,
25+
test: /\.jsx?$/,
2526
exclude: /node_modules/,
2627
use: ['babel-loader', 'eslint-loader']
2728
}
2829
]
2930
},
3031
plugins: [
3132
new HtmlWebpackPlugin({
32-
template: path.resolve('./index.html')
33+
template: 'index.html'
3334
}),
3435
new BundleAnalyzerPlugin({
3536
analyzerMode: 'disabled',
@@ -38,5 +39,8 @@ module.exports = {
3839
statsFilename: path.join(__dirname, 'stats/stats.json')
3940
})
4041
],
42+
resolve: {
43+
extensions: ['.js', '.jsx']
44+
},
4145
performance: { hints: false }
4246
};

0 commit comments

Comments
 (0)