Skip to content

Commit f1c6ea3

Browse files
arunodankzawa
authored andcommitted
Fix Jest example app (#458)
* Add a working JEST example app. * Add README.md * Move react-test-renderer to devDeps. * Add a newline to the .babelrc
1 parent 95eb20e commit f1c6ea3

File tree

7 files changed

+60
-42
lines changed

7 files changed

+60
-42
lines changed

examples/with-jest/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["next/babel"]
3+
}

examples/with-jest/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Example app using Jest to run tests
2+
3+
This example features:
4+
5+
* A properly configured Next.js app for Jest
6+
* An example test written with Jest Snapshot Testing
7+
* An example test written with Enzyme
8+
9+
## How to run it
10+
11+
```sh
12+
npm install
13+
npm run dev
14+
```
15+
16+
## Jest related info
17+
18+
After you've added `jest-cli` and `jest-babel` into your app, make sure to add the following `.babelrc` file.
19+
20+
```json
21+
{
22+
"presets": ["next/babel"]
23+
}
24+
```
25+
26+
It'll ask Jest to use the babel configurations used by Next.js.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports[`With Snapshot Testing App shows "Hello world!" 1`] = `
2+
<div>
3+
<p>
4+
Hello World!
5+
</p>
6+
</div>
7+
`;
+18-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
/* global it, expect */
1+
/* global it, expect, describe */
2+
23
import React from 'react'
34
import { shallow } from 'enzyme'
5+
import renderer from 'react-test-renderer'
46
import App from '../pages/index.js'
57

6-
it('App shows "Hello world!"', () => {
7-
const app = shallow(
8-
<App />
9-
)
8+
describe('With Enzyme', () => {
9+
it('App shows "Hello world!"', () => {
10+
const app = shallow(
11+
<App />
12+
)
13+
14+
expect(app.find('p').text()).toEqual('Hello World!')
15+
})
16+
})
1017

11-
expect(app.find('p').text()).toEqual('Hello world!')
18+
describe('With Snapshot Testing', () => {
19+
it('App shows "Hello world!"', () => {
20+
const component = renderer.create(<App />)
21+
const tree = component.toJSON()
22+
expect(tree).toMatchSnapshot()
23+
})
1224
})

examples/with-jest/package.json

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "my-app",
33
"dependencies": {
4-
"next": "^1.0.0"
4+
"next": "^2.0.0-beta"
55
},
66
"scripts": {
77
"test": "jest",
@@ -10,17 +10,12 @@
1010
"start": "next start"
1111
},
1212
"devDependencies": {
13-
"babel-jest": "^16.0.0",
13+
"babel-jest": "^18.0.0",
1414
"enzyme": "^2.5.1",
15-
"jest": "^16.0.2",
15+
"jest-cli": "^18.0.0",
1616
"react": "^15.3.2",
1717
"react-addons-test-utils": "^15.3.2",
18-
"react-dom": "^15.3.2"
19-
},
20-
"babel": {
21-
"presets": [
22-
"es2015",
23-
"react"
24-
]
18+
"react-dom": "^15.3.2",
19+
"react-test-renderer": "^15.4.1"
2520
}
2621
}

examples/with-jest/pages/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default () => (
22
<div>
3-
<p>Hello world!</p>
3+
<p>Hello World!</p>
44
</div>
55
)

examples/with-jest/readme.md

-25
This file was deleted.

0 commit comments

Comments
 (0)