Skip to content

Commit 13a736f

Browse files
author
Jared Palmer
committed
Add example with Jest
1 parent 903e17a commit 13a736f

File tree

10 files changed

+180
-24
lines changed

10 files changed

+180
-24
lines changed

examples/with-jest/.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"backpack-core/babel"
4+
]
5+
}

examples/with-jest/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
build
3+
npm-debug.log

examples/with-jest/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Example with Jest
2+
3+
## How to use
4+
5+
Download the example (or clone the whole project)[https://github.com/palmerhq/backpack.git]:
6+
7+
```bash
8+
curl https://codeload.github.com/palmerhq/backpack/tar.gz/master | tar -xz --strip=2 backpack-master/examples/with-jest
9+
cd with-jest
10+
```
11+
12+
Install it and run the tests:
13+
14+
```bash
15+
npm install
16+
npm t
17+
```
18+
19+
## Idea behind the example
20+
21+
This is an example of how to use the Jest test framework with Backpack.
22+
23+
**Points of Interest:**
24+
25+
- New `.babelrc` file with `presets: ["backpack-core/babel"]`
26+
- Adding `babel-jest` and `jest-cli` to devDependencies in `package.json`
27+
- Add `"jest"` section to `package.json` to manage Jest configuration
28+
- Demonstrates how to do an HTTP test with `supertest-as-promised`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* global it, expect, describe */
2+
3+
import createServer from '../src/createServer'
4+
import request from 'supertest'
5+
6+
const app = createServer()
7+
8+
describe('Backpack with Jest', () => {
9+
it('Object Rest Spread', () => {
10+
const hello = { hello: 'world' }
11+
const other = {...hello, nice: 'to meet you' }
12+
13+
expect(other).toEqual({ hello: 'world', nice: 'to meet you'})
14+
})
15+
16+
it('HTTP Request', async () => {
17+
const res = await request(app.listen())
18+
.get('/')
19+
expect(res.status).toBe(200)
20+
})
21+
})

examples/with-jest/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "backpack-examples-with-jest",
3+
"version": "0.0.3",
4+
"scripts": {
5+
"start": "node ./build/server/main.js",
6+
"test": "NODE_ENV=test jest",
7+
"dev": "backpack dev",
8+
"build": "backpack build",
9+
"postinstall": "backpack build"
10+
},
11+
"license": "MIT",
12+
"dependencies": {
13+
"express": "^4.14.0"
14+
},
15+
"devDependencies": {
16+
"babel-jest": "^18.0.0",
17+
"backpack-core": "0.0.3",
18+
"jest-cli": "^18.1.0",
19+
"supertest": "^2.0.1"
20+
},
21+
"jest": {
22+
"testEnvironment": "node",
23+
"transformIgnorePatterns": [
24+
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
25+
],
26+
"globals": {
27+
"__DEV__": true
28+
}
29+
}
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import express from 'express'
2+
3+
export default () => {
4+
const app = express()
5+
6+
app.get('/', async (req, res) => {
7+
try {
8+
const thing = await Promise.resolve({ one: 'two' }) // async/await!
9+
return res.json({...thing, hello: 'world'}) // object-rest-spread!
10+
} catch (e) {
11+
return res.json({ error: e.message })
12+
}
13+
})
14+
15+
return app
16+
}

examples/with-jest/src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import createServer from './createServer'
2+
3+
const port = process.env.PORT || 3000
4+
const app = createServer()
5+
6+
app.listen(port, (err) => {
7+
if (err) {
8+
console.error(err)
9+
}
10+
11+
if (__DEV__) { // webpack flags!
12+
console.log('> in development')
13+
}
14+
console.log(`> listening on port ${port}`)
15+
})
16+
17+
18+

packages/backpack-core/babel.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const path = require('path')
2+
3+
const preset = {
4+
presets: [
5+
[require('babel-preset-env').default, {
6+
'target': {
7+
'node': 'current'
8+
},
9+
"modules": false
10+
}]
11+
],
12+
plugins: [
13+
// class { handleThing = () => { } }
14+
require.resolve('babel-plugin-transform-class-properties'),
15+
16+
// async => (..)
17+
require.resolve('babel-plugin-transform-async-to-generator'),
18+
19+
// The following two plugins use Object.assign directly, instead of Babel's
20+
// extends helper. Note that this assumes `Object.assign` is available.
21+
// { ...todo, completed: true }
22+
[require.resolve('babel-plugin-transform-object-rest-spread'), {
23+
useBuiltIns: true
24+
}],
25+
26+
[require.resolve('babel-plugin-transform-runtime'), {
27+
helpers: false,
28+
polyfill: false,
29+
regenerator: true,
30+
// Resolve the Babel runtime relative to the config.
31+
moduleName: path.dirname(require.resolve('babel-runtime/package'))
32+
}]
33+
]
34+
}
35+
36+
if (process.env.NODE_ENV || process.env.BABEL_ENV === 'test') {
37+
preset.plugins.push.apply(preset.plugins, [
38+
// We always include this plugin regardless of environment
39+
// because of a Babel bug that breaks object rest/spread without it:
40+
// https://github.com/babel/babel/issues/4851
41+
require.resolve('babel-plugin-transform-es2015-parameters'),
42+
// Jest needs this to work properly with import/export syntax
43+
require.resolve('babel-plugin-transform-es2015-modules-commonjs')
44+
])
45+
}
46+
47+
module.exports = preset

packages/backpack-core/config/webpack.config.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const nodeExternals = require('webpack-node-externals')
33
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
44
const config = require('./paths')
55
const path = require('path')
6+
const babelConfig = require('../babel')
67

78
// This is the Webpack configuration.
89
// It is focused on developer experience and fast rebuilds.
@@ -74,26 +75,7 @@ module.exports = (options) => ({
7475
/node_modules/,
7576
config.buildPath
7677
],
77-
options: {
78-
// babel-preset-env is like autoprefixer, but for javascript.
79-
// It efficiently optimizes transpilation based on the specified
80-
// target environment. As a default, we set it to target the
81-
// user's currently installed Node.js version. We also turn off
82-
// ES Modules, and Webpack handles that for us.
83-
presets: [
84-
[require.resolve('babel-preset-env'), {
85-
target: {
86-
node: 'current'
87-
},
88-
modules: false
89-
}]
90-
],
91-
// These are the default JavaScript language addons.
92-
plugins: [
93-
require.resolve('babel-plugin-transform-object-rest-spread'),
94-
require.resolve('babel-plugin-transform-class-properties')
95-
]
96-
}
78+
options: babelConfig
9779
}
9880
]
9981
},
@@ -115,9 +97,10 @@ module.exports = (options) => ({
11597
// The FriendlyErrorsWebpackPlugin (when combined with source-maps)
11698
// gives Backpack its human-readable error messages.
11799
new FriendlyErrorsWebpackPlugin(),
118-
// This plugin is awkwardly named. It does not actually swallow errors.
119-
// Instead, it just prevents Webpack from printing out compile time
120-
// stats to the console.
100+
// This plugin is awkwardly named. Use to be called NoErrorsPlugin.
101+
// It does not actually swallow errors. Instead, it just prevents
102+
// Webpack from printing out compile time stats to the console.
103+
// @todo new webpack.NoEmitOnErrorsPlugin()
121104
new webpack.NoErrorsPlugin()
122105
]
123106
})

packages/backpack-core/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
"dependencies": {
1111
"babel-core": "^6.21.0",
1212
"babel-loader": "^6.2.10",
13+
"babel-plugin-transform-async-to-generator": "^6.16.0",
1314
"babel-plugin-transform-class-properties": "^6.19.0",
15+
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
16+
"babel-plugin-transform-es2015-parameters": "^6.21.0",
1417
"babel-plugin-transform-object-rest-spread": "^6.20.2",
18+
"babel-plugin-transform-runtime": "^6.15.0",
1519
"babel-polyfill": "^6.20.0",
1620
"babel-preset-env": "^1.1.4",
1721
"chokidar": "^1.6.1",
1822
"cross-spawn": "^5.0.1",
23+
"dotenv": "^4.0.0",
1924
"friendly-errors-webpack-plugin": "^1.1.2",
2025
"nodemon": "^1.11.0",
2126
"ramda": "^0.23.0",
2227
"source-map-support": "^0.4.8",
23-
"webpack": "2.2.0-rc.3",
28+
"webpack": "^2.2.0-rc.3",
2429
"webpack-node-externals": "^1.5.4"
2530
}
2631
}

0 commit comments

Comments
 (0)