Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Karma + Mocha with Jest #824

Merged
merged 42 commits into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a529975
refactor: replace karma and mocha with jest
eddyerburgh Jul 25, 2017
de30cab
refactor: remove webpack.test.conf
eddyerburgh Jul 25, 2017
3af6a44
refactor: prompt jest instead of mocha and karma
eddyerburgh Jul 25, 2017
abbd2c5
feat(jest): add coverage reporting
eddyerburgh Jul 25, 2017
6263bce
docs(jest): update unit.md with details of jest
eddyerburgh Jul 25, 2017
636e3b2
docs(jest): remove karma files from structure.md
eddyerburgh Jul 25, 2017
56427e7
docs(jest): replace details on Karma with details on Jest
eddyerburgh Jul 25, 2017
e7ec3b1
docs: add missing period
eddyerburgh Jul 25, 2017
48f89ec
docs(jest): replace karma with Jest in commands.md
eddyerburgh Jul 25, 2017
0744aa1
docs(jest): update npm run unit bullet points
eddyerburgh Jul 25, 2017
f6541fc
refactor: use module-resolver in place of jest moduleNameMapper
eddyerburgh Jul 25, 2017
de0ce71
refactor: remove .vue extension from Hello import
eddyerburgh Jul 25, 2017
f52e256
test: include src files in test coverage
eddyerburgh Jul 25, 2017
21bfde6
test: only ignore router/index in coverage if router option selected
eddyerburgh Jul 25, 2017
84ab9d0
chore: fix merge conflicts
eddyerburgh Sep 24, 2017
823e3c3
Merge branch 'master' into master
eddyerburgh Sep 24, 2017
d117247
chore: add MIT free Jest
eddyerburgh Sep 27, 2017
415caed
Reference dev script in start script instead of copy pasting (#894)
robwierzbowski Oct 3, 2017
cd78d3a
feat: add karma option
eddyerburgh Oct 5, 2017
d8ffc52
Merge branch 'master' into master
eddyerburgh Oct 5, 2017
2eaf47d
refactor: use const in webpack.test.conf
eddyerburgh Oct 5, 2017
fd9c3ef
Release 1.1.0 (Merge from develop)
LinusBorg Oct 7, 2017
0418571
add version tag to config/index.js
LinusBorg Oct 7, 2017
8bc554c
Merge branch 'develop'
LinusBorg Oct 8, 2017
be3efc0
stick to ES5
LinusBorg Oct 11, 2017
a355ee8
Bumping Vue+VueRouter versions, some minor fixes. (#986)
LinusBorg Oct 16, 2017
6043118
Fix missing dependency bumps (#987)
LinusBorg Oct 16, 2017
cfe2cc2
refactor: use jest module mapping
eddyerburgh Nov 4, 2017
27f79e8
feat(jest): filter jest setup file
eddyerburgh Nov 4, 2017
6710dad
merge: merge develop branch
eddyerburgh Nov 4, 2017
055a3a0
Merge branch 'master' into master
eddyerburgh Nov 4, 2017
d8fd92a
Merge branch 'master' into master
eddyerburgh Nov 4, 2017
dd21933
fix: fix trailing comma
eddyerburgh Nov 4, 2017
de8ed39
merge develop branch
eddyerburgh Nov 11, 2017
78fd900
docs: add Jest and Karma to unit section
eddyerburgh Nov 11, 2017
d16a553
fix: add Jest options if jest option
eddyerburgh Nov 11, 2017
e396d70
test: change env in .eslintrc
eddyerburgh Nov 11, 2017
0867775
fix: remove merge trace
eddyerburgh Nov 11, 2017
3625080
fix: merge package.json with develop
eddyerburgh Nov 12, 2017
6f5d88d
docs: split unit test docs into Jest and Karma
eddyerburgh Nov 12, 2017
a35c931
docs: add Karma and Jest to README
eddyerburgh Nov 12, 2017
e817d3e
docs: reimplement karma files in structure.md
eddyerburgh Nov 12, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ The development server will run on port 8080 by default. If that port is already
- 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.
- Use `npm run build --report`to build with bundle size analytics.

- `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).
- `npm run unit`: Unit tests run in [JSDOM](https://github.com/tmpvar/jsdom) with [Jest](https://facebook.github.io/jest/).
- Supports ES2015+ in test files.
- Supports all webpack loaders.
- Easy mock injection.
- Easy mocking.

- `npm run e2e`: End-to-end tests with [Nightwatch](http://nightwatchjs.org/).
- Run tests in multiple browsers in parallel.
Expand Down
5 changes: 2 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ All build commands are executed via [NPM Scripts](https://docs.npmjs.com/misc/sc

### `npm run unit`

> Run unit tests in PhantomJS with [Karma](https://karma-runner.github.io/). See [Unit Testing](unit.md) for more details.
> Run unit tests in JSDOM with [Jest](https://facebook.github.io/jest/docs/getting-started.html). See [Unit Testing](unit.md) for more details.

- Supports ES2015+ in test files.
- Supports all webpack loaders.
- Easy [mock injection](http://vuejs.github.io/vue-loader/en/workflow/testing-with-mocks.html).
- Easy [mocking](https://facebook.github.io/jest/docs/mock-functions.html).

### `npm run e2e`

Expand Down
4 changes: 1 addition & 3 deletions docs/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
├── static/ # pure static assets (directly copied)
├── test/
│ └── unit/ # unit tests
│ │   ├── specs/ # test spec files
│ │   ├── index.js # test build entry file
│ │   └── karma.conf.js # test runner config file
│ │   └── specs/ # test spec files
│ └── e2e/ # e2e tests
│ │   ├── specs/ # test spec files
│ │   ├── custom-assertions/ # custom assertions for e2e tests
Expand Down
28 changes: 2 additions & 26 deletions docs/unit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,8 @@

An overview of the tools used by this boilerplate for unit testing:

- [Karma](https://karma-runner.github.io/): the test runner that launches browsers, runs the tests and reports the results to us.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyerburgh Since we are leaving the karma setup available, I don't think we should remove this docs section.

We should rather leave that docs in, but prepend the page with new infos about jest.

- [karma-webpack](https://github.com/webpack/karma-webpack): the plugin for Karma that bundles our tests using Webpack.
- [Mocha](https://mochajs.org/): the test framework that we write test specs with.
- [Chai](http://chaijs.com/): test assertion library that provides better assertion syntax.
- [Sinon](http://sinonjs.org/): test utility library that provides spies, stubs and mocks.

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.

And the files:

- `index.js`

This is the entry file used by `karma-webpack` to bundle all the test code and source code (for coverage purposes). You can ignore it for the most part.

- `specs/`

This directory is where you write your actual tests. You can use full ES2015+ and all supported Webpack loaders in your tests.

- `karma.conf.js`

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

## Running Tests in More Browsers

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`.
- [Jest](https://facebook.github.io/jest/): the test runner that launches JSDOM runs the tests and reports the results to us.

## Mocking Dependencies

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).
Jest comes with the ability to mock dependencies. See the [mock functions guide](https://facebook.github.io/jest/docs/mock-functions.html) for more details.
29 changes: 27 additions & 2 deletions meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,29 @@ module.exports = {
},
"unit": {
"type": "confirm",
"message": "Setup unit tests with Karma + Mocha?"
"message": "Setup unit tests"
},
"runner": {
"when": "unit",
"type": "list",
"message": "Pick a test runner",
"choices": [
{
"name": "Jest",
"value": "jest",
"short": "jest"
},
{
"name": "Karma and Mocha",
"value": "karma",
"short": "karma"
},
{
"name": "none (configure it yourself)",
"value": "noTest",
"short": "noTest"
}
]
},
"e2e": {
"type": "confirm",
Expand All @@ -84,7 +106,10 @@ module.exports = {
".eslintignore": "lint",
"config/test.env.js": "unit || e2e",
"test/unit/**/*": "unit",
"build/webpack.test.conf.js": "unit",
"test/unit/index.js": "runner === 'karma'",
"test/unit/karma.conf.js": "runner === 'karma'",
"test/unit/specs/index.js": "runner === 'karma'",
"test/unit/setup.js": "runner === 'jest'",
"test/e2e/**/*": "e2e",
"src/router/**/*": "router"
},
Expand Down
3 changes: 2 additions & 1 deletion template/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"plugins": ["transform-runtime"],
"env": {
"test": {
"presets": ["env", "stage-2"],
"presets": ["env", "stage-2"]{{#if_eq runner "karma"}},
"plugins": ["istanbul"]
{{/if_eq}}
}
}
}
1 change: 1 addition & 0 deletions template/config/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

'use strict'
// Template version: 1.1.3
// see http://vuejs-templates.github.io/webpack for documentation.
Expand Down
66 changes: 55 additions & 11 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@
"author": "{{ author }}",
"private": true,
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"dev": "node build/dev-server.js",
Copy link
Contributor

@LinusBorg LinusBorg Nov 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that merge doesn't look like it's supposed to I think ... the webpack-dev-server is new in the developbranch, while yours was still built on the old dev-server - so the former should stay in and the latter should go out.

Similarly, the other additions to package.json down in this file are all "bringing back" dependencies of the old dev-server.

But we might simply resolve this during / after the merge ..

"start": "npm run dev",
"build": "node build/build.js"{{#unit}},
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run"{{/unit}}{{#e2e}},
"e2e": "node test/e2e/runner.js"{{/e2e}}{{#if_or unit e2e}},
"test": "{{#unit}}npm run unit{{/unit}}{{#unit}}{{#e2e}} && {{/e2e}}{{/unit}}{{#e2e}}npm run e2e{{/e2e}}"{{/if_or}}{{#lint}},
"lint": "eslint --ext .js,.vue src{{#unit}} test/unit/specs{{/unit}}{{#e2e}} test/e2e/specs{{/e2e}}"{{/lint}}
{{#if_eq runner "jest"}}
"unit": "jest test/unit/specs --coverage",
{{/if_eq}}
{{#if_eq runner "karma"}}
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
{{/if_eq}}
{{#e2e}}
"e2e": "node test/e2e/runner.js",{{/e2e}}
{{#if_or unit e2e}}
"test": "{{#unit}}npm run unit{{/unit}}{{#unit}}{{#e2e}} && {{/e2e}}{{/unit}}{{#e2e}}npm run e2e{{/e2e}}",
{{/if_or}}
{{#lint}}
"lint": "eslint --ext .js,.vue src{{#unit}} test/unit/specs{{/unit}}{{#e2e}} test/e2e/specs{{/e2e}}",
{{/lint}}
"build": "node build/build.js"
},
"dependencies": {
"vue": "^2.5.2"{{#router}},
Expand All @@ -29,6 +39,7 @@
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
"chalk": "^2.0.1",
"connect-history-api-fallback": "^1.3.0",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.28.0",
{{#lint}}
Expand All @@ -50,12 +61,19 @@
{{/if_eq}}
{{/lint}}
"eventsource-polyfill": "^0.9.6",
"express": "^4.14.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.4",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.17.3",
"webpack-bundle-analyzer": "^2.9.0",
{{#unit}}
{{#if_eq runner "jest"}}
"babel-jest": "^21.0.2",
"jest": "^21.2.0",
"vue-jest": "^1.0.1",
{{/if_eq}}
{{#if_eq runner "karma"}}
"cross-env": "^5.0.1",
"karma": "^1.4.1",
"karma-coverage": "^1.1.1",
Expand All @@ -73,8 +91,7 @@
"inject-loader": "^3.0.0",
"babel-plugin-istanbul": "^4.1.1",
"phantomjs-prebuilt": "^2.1.14",
{{/unit}}
"node-notifier": "^5.1.2",
{{/if_eq}}
{{#e2e}}
"chromedriver": "^2.27.2",
"cross-spawn": "^5.0.1",
Expand All @@ -83,19 +100,46 @@
{{/e2e}}
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"opn": "^5.1.0",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.2.0",
"postcss-loader": "^2.0.8",
"rimraf": "^2.6.0",
"url-loader": "^0.5.8",
"vue-loader": "^13.3.0",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.5.2",
"portfinder": "^1.0.13",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"webpack-dev-middleware": "^1.12.0",
"webpack-hot-middleware": "^2.18.2",
"webpack-merge": "^4.1.0"
},
{{#unit}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't that be {{#if_eq runner "jest"}}?

"jest": {
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
},
"setupFiles": ["<rootDir>/test/unit/setup"],
"mapCoverage": true,
"collectCoverageFrom" : [
"src/**/*.{js,vue}",
"!src/main.js",
{{#router}}
"!src/router/index.js",
{{/router}}
"!**/node_modules/**"
]
},
{{/unit}}
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
Expand Down
3 changes: 0 additions & 3 deletions template/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ export default {
h1, h2 {
font-weight: normal;
}

ul {
list-style-type: none;
padding: 0;
}

li {
display: inline-block;
margin: 0 10px;
}

a {
color: #42b983;
}
Expand Down
4 changes: 4 additions & 0 deletions template/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export default new Router({
routes: [
{
path: '/',
<<<<<<< HEAD
name: 'HelloWorld',
=======
name: 'Hello',
>>>>>>> 89c6fb18d4640afd6a5e89106b2493b4465d36a3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have some leftovers from an older merge conflict...

component: HelloWorld{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
}{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
]{{#if_eq lintConfig "airbnb"}},{{/if_eq}}
Expand Down
6 changes: 1 addition & 5 deletions template/test/unit/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"env": {
"mocha": true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we are leaving the karma/mocha setup in place, the eslint config should probably also leave this in.

It should be conditional:

{
  "env": {{{#if_eq runner "karma"}}
    "mocha": true{{/if_eq}}{{#if_eq runner "jest"}}
    "jest"{{/if_eq}}
  }{{#if_eq runner "karma"}},
  "globals": {
    "expect": true,
    "sinon": true,
    "jest": true
  }{{/if_eq}}
}

},
"globals": {
"expect": true,
"sinon": true
"jest": true
}
}
3 changes: 3 additions & 0 deletions template/test/unit/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Vue from 'vue'

Vue.config.productionTip = false
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ describe('HelloWorld.vue', () => {
const Constructor = Vue.extend(HelloWorld){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
const vm = new Constructor().$mount(){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
expect(vm.$el.querySelector('.hello h1').textContent)
.to.equal('Welcome to Your Vue.js App'){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
{{#if_eq runner "karma"}}.to.equal('Welcome to Your Vue.js App'){{#if_eq lintConfig "airbnb"}};{{/if_eq}}{{/if_eq}}{{#if_eq runner "jest"}}.toEqual('Welcome to Your Vue.js App'){{#if_eq lintConfig "airbnb"}};{{/if_eq}}{{/if_eq}}
}){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
}){{#if_eq lintConfig "airbnb"}};{{/if_eq}}
Empty file modified test.sh
100644 → 100755
Empty file.