Skip to content

Commit e2d331d

Browse files
stephensaucedatimneutkens
authored andcommittedSep 16, 2018
add mocha example (#5182)
Adding an example of testing with Mocha #4767
1 parent 35c2103 commit e2d331d

File tree

7 files changed

+116
-0
lines changed

7 files changed

+116
-0
lines changed
 

‎examples/with-mocha/.babelrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"development": {
4+
"presets": ["next/babel"]
5+
},
6+
"production": {
7+
"presets": ["next/babel"]
8+
},
9+
"test": {
10+
"presets": [["next/babel", { "preset-env": { "modules": "commonjs" } }]]
11+
}
12+
}
13+
}

‎examples/with-mocha/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-mocha)
2+
3+
# Example app with Mocha tests
4+
5+
## How to use
6+
7+
### Using `create-next-app`
8+
9+
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
10+
11+
```bash
12+
npx create-next-app --example with-mocha with-mocha-app
13+
# or
14+
yarn create next-app --example with-mocha with-mocha-app
15+
```
16+
17+
### Download manually
18+
19+
Download the example:
20+
21+
```bash
22+
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-mocha
23+
cd with-mocha
24+
```
25+
26+
Install it and run:
27+
28+
```bash
29+
npm install
30+
npm run dev
31+
# or
32+
yarn
33+
yarn dev
34+
```
35+
36+
## Run Mocha tests
37+
38+
```bash
39+
npm run test
40+
# or
41+
yarn test
42+
```
43+
44+
## The idea behind the example
45+
46+
This example features:
47+
48+
* An app with Mocha tests
49+
50+
> A very important part of this example is the `.babelrc` file which configures the `test` environment to use `babel-preset-env` and configures it to transpile modules to `commonjs`). [Learn more](https://github.com/zeit/next.js/issues/2895).

‎examples/with-mocha/mocha.setup.js

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

‎examples/with-mocha/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "with-mocha",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"test": "NODE_ENV=test mocha",
6+
"dev": "next",
7+
"build": "next build",
8+
"start": "next start"
9+
},
10+
"dependencies": {
11+
"next": "latest",
12+
"react": "^16.5.0",
13+
"react-dom": "^16.5.0"
14+
},
15+
"devDependencies": {
16+
"@babel/core": "^7.0.1",
17+
"@babel/register": "^7.0.0",
18+
"enzyme": "^3.6.0",
19+
"enzyme-adapter-react-16": "^1.5.0",
20+
"expect.js": "^0.3.1",
21+
"mocha": "^5.2.0"
22+
}
23+
}

‎examples/with-mocha/pages/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default () => (
2+
<div>
3+
<style jsx>{`
4+
p {
5+
color: red;
6+
}
7+
`}</style>
8+
<p>Hello World!</p>
9+
</div>
10+
)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* global describe, it */
2+
import { shallow } from 'enzyme'
3+
import React from 'react'
4+
import expect from 'expect.js'
5+
6+
import App from '../pages/index.js'
7+
8+
describe('With Enzyme', () => {
9+
it('App shows "Hello world!"', () => {
10+
const app = shallow(<App />)
11+
12+
expect(app.find('p').text()).to.equal('Hello World!')
13+
})
14+
})

‎examples/with-mocha/test/mocha.opts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--require @babel/register
2+
--file mocha.setup.js

0 commit comments

Comments
 (0)
Please sign in to comment.