Skip to content

Commit cdb3710

Browse files
eddyerburghLinusBorg
authored andcommitted
Add Jest as new unit test runner, keep Karma + Mocha as a fallback (#824)
* refactor: replace karma and mocha with jest * refactor: remove webpack.test.conf * refactor: prompt jest instead of mocha and karma * feat(jest): add coverage reporting * docs(jest): update unit.md with details of jest * docs(jest): remove karma files from structure.md * docs(jest): replace details on Karma with details on Jest * docs: add missing period * docs(jest): replace karma with Jest in commands.md * docs(jest): update npm run unit bullet points * refactor: use module-resolver in place of jest moduleNameMapper * refactor: remove .vue extension from Hello import * test: include src files in test coverage * test: only ignore router/index in coverage if router option selected * chore: fix merge conflicts * chore: add MIT free Jest * Reference dev script in start script instead of copy pasting (#894) Less duplication FTW. * feat: add karma option * refactor: use const in webpack.test.conf * add version tag to config/index.js * stick to ES5 this file is not transpiled * Bumping Vue+VueRouter versions, some minor fixes. (#986) * switch hello-world tag to PascalCase (#951) * fix Es6 code issue. This file is not transpiled, so we should stick to ES5 * Fix bug in dev-server when a proxyTable entry is a string (#965) `options` can not be a `const` because it is modified if value is a string. * Fix casing * Revert 9befbfc * update to vue and vue-router latest version (#984) - vue 2.5 - vue-router 3.0 * Fix missing dependency bumps (#987) * switch hello-world tag to PascalCase (#951) * fix Es6 code issue. This file is not transpiled, so we should stick to ES5 * Fix bug in dev-server when a proxyTable entry is a string (#965) `options` can not be a `const` because it is modified if value is a string. * Fix casing * Revert 9befbfc * update to vue and vue-router latest version (#984) - vue 2.5 - vue-router 3.0 * fix missing dependency updates for * vue-template compiler * vue-loader * bump version string * refactor: use jest module mapping * feat(jest): filter jest setup file * fix: fix trailing comma * docs: add Jest and Karma to unit section * fix: add Jest options if jest option * test: change env in .eslintrc * fix: remove merge trace * fix: merge package.json with develop * docs: split unit test docs into Jest and Karma * docs: add Karma and Jest to README * docs: reimplement karma files in structure.md
1 parent 1cbcde1 commit cdb3710

File tree

14 files changed

+121
-34
lines changed

14 files changed

+121
-34
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ The development server will run on port 8080 by default. If that port is already
3939
- Static assets compiled with version hashes for efficient long-term caching, and an auto-generated production `index.html` with proper URLs to these generated assets.
4040
- Use `npm run build --report`to build with bundle size analytics.
4141

42-
- `npm run unit`: Unit tests run in PhantomJS with [Karma](http://karma-runner.github.io/0.13/index.html) + [Mocha](http://mochajs.org/) + [karma-webpack](https://github.com/webpack/karma-webpack).
42+
- `npm run unit`: Unit tests run in [JSDOM](https://github.com/tmpvar/jsdom) with [Jest](https://facebook.github.io/jest/), or in PhantomJS with Karma + Mocha + karma-webpack.
4343
- Supports ES2015+ in test files.
44-
- Supports all webpack loaders.
45-
- Easy mock injection.
44+
- Easy mocking.
4645

4746
- `npm run e2e`: End-to-end tests with [Nightwatch](http://nightwatchjs.org/).
4847
- Run tests in multiple browsers in parallel.

docs/commands.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ All build commands are executed via [NPM Scripts](https://docs.npmjs.com/misc/sc
2323

2424
### `npm run unit`
2525

26-
> Run unit tests in PhantomJS with [Karma](https://karma-runner.github.io/). See [Unit Testing](unit.md) for more details.
26+
> Run unit tests in JSDOM with [Jest](https://facebook.github.io/jest/docs/getting-started.html). See [Unit Testing](unit.md) for more details.
2727
2828
- Supports ES2015+ in test files.
29-
- Supports all webpack loaders.
30-
- Easy [mock injection](http://vuejs.github.io/vue-loader/en/workflow/testing-with-mocks.html).
29+
- Easy [mocking](https://facebook.github.io/jest/docs/mock-functions.html).
3130

3231
### `npm run e2e`
3332

docs/structure.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
├── static/ # pure static assets (directly copied)
1818
├── test/
1919
│ └── unit/ # unit tests
20-
│ │   ├── specs/ # test spec files
21-
│ │   ├── index.js # test build entry file
22-
│ │   └── karma.conf.js # test runner config file
20+
│ │ ├── specs/ # test spec files
21+
│ │ ├── setup.js # file that runs before Jest tests
22+
│ │ ├── index.js # test build entry file
23+
│ │ └── karma.conf.js # test runner config file
2324
│ └── e2e/ # e2e tests
2425
│ │   ├── specs/ # test spec files
2526
│ │   ├── custom-assertions/ # custom assertions for e2e tests

docs/unit.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
# Unit Testing
22

3+
This project offers two options for unit testing—Jest, and Karma and Mocha.
4+
35
An overview of the tools used by this boilerplate for unit testing:
46

7+
## Jest
8+
9+
- [Jest](https://facebook.github.io/jest/): the test runner that launches JSDOM runs the tests and reports the results to us.
10+
11+
### Files
12+
13+
- `setup.js`
14+
15+
Jest runs this file before it runs the unit tests. It sets the Vue production tip to false.
16+
17+
### Mocking Dependencies
18+
19+
The Jest boilerplate comes with the ability to mock dependencies. See the [mock functions guide](https://facebook.github.io/jest/docs/mock-functions.html) for more details.
20+
21+
## Karma and Mocha
22+
523
- [Karma](https://karma-runner.github.io/): the test runner that launches browsers, runs the tests and reports the results to us.
624
- [karma-webpack](https://github.com/webpack/karma-webpack): the plugin for Karma that bundles our tests using Webpack.
725
- [Mocha](https://mochajs.org/): the test framework that we write test specs with.
@@ -10,7 +28,7 @@ An overview of the tools used by this boilerplate for unit testing:
1028

1129
Chai and Sinon are integrated using [karma-sinon-chai](https://github.com/kmees/karma-sinon-chai), so all Chai interfaces (`should`, `expect`, `assert`) and `sinon` are globally available in test files.
1230

13-
And the files:
31+
### Files
1432

1533
- `index.js`
1634

@@ -24,10 +42,10 @@ And the files:
2442

2543
This is the Karma configuration file. See [Karma docs](https://karma-runner.github.io/) for more details.
2644

27-
## Running Tests in More Browsers
45+
### Running Tests in More Browsers
2846

2947
You can run the tests in multiple real browsers by installing more [karma launchers](https://karma-runner.github.io/1.0/config/browsers.html) and adjusting the `browsers` field in `test/unit/karma.conf.js`.
3048

31-
## Mocking Dependencies
49+
### Mocking Dependencies
3250

33-
This boilerplate comes with [inject-loader](https://github.com/plasticine/inject-loader) installed by default. For usage with `*.vue` components, see [vue-loader docs on testing with mocks](http://vue-loader.vuejs.org/en/workflow/testing-with-mocks.html).
51+
The Karma unit test boilerplate comes with [inject-loader](https://github.com/plasticine/inject-loader) installed by default. For usage with `*.vue` components, see [vue-loader docs on testing with mocks](http://vue-loader.vuejs.org/en/workflow/testing-with-mocks.html).

meta.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,29 @@ module.exports = {
7272
},
7373
"unit": {
7474
"type": "confirm",
75-
"message": "Setup unit tests with Karma + Mocha?"
75+
"message": "Setup unit tests"
76+
},
77+
"runner": {
78+
"when": "unit",
79+
"type": "list",
80+
"message": "Pick a test runner",
81+
"choices": [
82+
{
83+
"name": "Jest",
84+
"value": "jest",
85+
"short": "jest"
86+
},
87+
{
88+
"name": "Karma and Mocha",
89+
"value": "karma",
90+
"short": "karma"
91+
},
92+
{
93+
"name": "none (configure it yourself)",
94+
"value": "noTest",
95+
"short": "noTest"
96+
}
97+
]
7698
},
7799
"e2e": {
78100
"type": "confirm",
@@ -84,7 +106,10 @@ module.exports = {
84106
".eslintignore": "lint",
85107
"config/test.env.js": "unit || e2e",
86108
"test/unit/**/*": "unit",
87-
"build/webpack.test.conf.js": "unit",
109+
"test/unit/index.js": "runner === 'karma'",
110+
"test/unit/karma.conf.js": "runner === 'karma'",
111+
"test/unit/specs/index.js": "runner === 'karma'",
112+
"test/unit/setup.js": "runner === 'jest'",
88113
"test/e2e/**/*": "e2e",
89114
"src/router/**/*": "router"
90115
},

template/.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"plugins": ["transform-runtime"],
99
"env": {
1010
"test": {
11-
"presets": ["env", "stage-2"],
11+
"presets": ["env", "stage-2"]{{#if_eq runner "karma"}},
1212
"plugins": ["istanbul"]
13+
{{/if_eq}}
1314
}
1415
}
1516
}

template/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
'use strict'
23
// Template version: 1.1.3
34
// see http://vuejs-templates.github.io/webpack for documentation.

template/package.json

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77
"scripts": {
88
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
99
"start": "npm run dev",
10-
"build": "node build/build.js"{{#unit}},
11-
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run"{{/unit}}{{#e2e}},
12-
"e2e": "node test/e2e/runner.js"{{/e2e}}{{#if_or unit e2e}},
13-
"test": "{{#unit}}npm run unit{{/unit}}{{#unit}}{{#e2e}} && {{/e2e}}{{/unit}}{{#e2e}}npm run e2e{{/e2e}}"{{/if_or}}{{#lint}},
14-
"lint": "eslint --ext .js,.vue src{{#unit}} test/unit/specs{{/unit}}{{#e2e}} test/e2e/specs{{/e2e}}"{{/lint}}
10+
{{#if_eq runner "jest"}}
11+
"unit": "jest test/unit/specs --coverage",
12+
{{/if_eq}}
13+
{{#if_eq runner "karma"}}
14+
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
15+
{{/if_eq}}
16+
{{#e2e}}
17+
"e2e": "node test/e2e/runner.js",{{/e2e}}
18+
{{#if_or unit e2e}}
19+
"test": "{{#unit}}npm run unit{{/unit}}{{#unit}}{{#e2e}} && {{/e2e}}{{/unit}}{{#e2e}}npm run e2e{{/e2e}}",
20+
{{/if_or}}
21+
{{#lint}}
22+
"lint": "eslint --ext .js,.vue src{{#unit}} test/unit/specs{{/unit}}{{#e2e}} test/e2e/specs{{/e2e}}",
23+
{{/lint}}
24+
"build": "node build/build.js"
1525
},
1626
"dependencies": {
1727
"vue": "^2.5.2"{{#router}},
@@ -50,12 +60,18 @@
5060
{{/if_eq}}
5161
{{/lint}}
5262
"eventsource-polyfill": "^0.9.6",
63+
"express": "^4.14.1",
5364
"extract-text-webpack-plugin": "^3.0.0",
5465
"file-loader": "^1.1.4",
5566
"friendly-errors-webpack-plugin": "^1.6.1",
5667
"html-webpack-plugin": "^2.30.1",
5768
"webpack-bundle-analyzer": "^2.9.0",
58-
{{#unit}}
69+
{{#if_eq runner "jest"}}
70+
"babel-jest": "^21.0.2",
71+
"jest": "^21.2.0",
72+
"vue-jest": "^1.0.2",
73+
{{/if_eq}}
74+
{{#if_eq runner "karma"}}
5975
"cross-env": "^5.0.1",
6076
"karma": "^1.4.1",
6177
"karma-coverage": "^1.1.1",
@@ -73,7 +89,7 @@
7389
"inject-loader": "^3.0.0",
7490
"babel-plugin-istanbul": "^4.1.1",
7591
"phantomjs-prebuilt": "^2.1.14",
76-
{{/unit}}
92+
{{/if_eq}}
7793
"node-notifier": "^5.1.2",
7894
{{#e2e}}
7995
"chromedriver": "^2.27.2",
@@ -85,7 +101,6 @@
85101
"shelljs": "^0.7.6",
86102
"optimize-css-assets-webpack-plugin": "^3.2.0",
87103
"ora": "^1.2.0",
88-
"postcss-loader": "^2.0.8",
89104
"rimraf": "^2.6.0",
90105
"url-loader": "^0.5.8",
91106
"vue-loader": "^13.3.0",
@@ -96,6 +111,32 @@
96111
"webpack-dev-server": "^2.9.1",
97112
"webpack-merge": "^4.1.0"
98113
},
114+
{{#if_eq runner "jest"}}
115+
"jest": {
116+
"moduleFileExtensions": [
117+
"js",
118+
"json",
119+
"vue"
120+
],
121+
"moduleNameMapper": {
122+
"^@/(.*)$": "<rootDir>/src/$1"
123+
},
124+
"transform": {
125+
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
126+
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
127+
},
128+
"setupFiles": ["<rootDir>/test/unit/setup"],
129+
"mapCoverage": true,
130+
"collectCoverageFrom" : [
131+
"src/**/*.{js,vue}",
132+
"!src/main.js",
133+
{{#router}}
134+
"!src/router/index.js",
135+
{{/router}}
136+
"!**/node_modules/**"
137+
]
138+
},
139+
{{/if_eq}}
99140
"engines": {
100141
"node": ">= 4.0.0",
101142
"npm": ">= 3.0.0"

template/src/components/HelloWorld.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@ export default {
3636
h1, h2 {
3737
font-weight: normal;
3838
}
39-
4039
ul {
4140
list-style-type: none;
4241
padding: 0;
4342
}
44-
4543
li {
4644
display: inline-block;
4745
margin: 0 10px;
4846
}
49-
5047
a {
5148
color: #42b983;
5249
}

template/src/router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default new Router({
88
routes: [
99
{
1010
path: '/',
11-
name: 'Hello',
11+
name: 'HelloWorld',
1212
component: HelloWorld{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
1313
}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
1414
]{{#if_eq lintConfig "airbnb"}},{{/if_eq}}

0 commit comments

Comments
 (0)