Skip to content

Commit d9f50e0

Browse files
committed
finish docs
1 parent dcb164b commit d9f50e0

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

docs/workflow/production.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module.exports = {
3232

3333
Obviously we don't want to use this config during development, so there are several ways to approach this:
3434

35-
1. Dynamically build up the configuration object based on an environment variable, as shown in [vue-loader-example](https://github.com/vuejs/vue-loader-example/blob/master/webpack.config.js#L40-L56);
35+
1. Dynamically build up the configuration object based on an environment variable;
3636

37-
2. Use two separate Webpack config files, one for development and one for production. And maybe share some common options between them in a third file.
37+
2. Or, use two separate Webpack config files, one for development and one for production. And maybe share some common options between them in a third file, as shown in [vue-loader-example](https://github.com/vuejs/vue-loader-example/tree/master/build).
3838

3939
It's really up to you as long as it achieves the goal.

docs/workflow/testing.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,93 @@
11
# Testing
22

3-
### Karma + karma-webpack
3+
When testing `*.vue` files, we cannot use a plain CommonJS-based test runner because it won't know how to handle `*.vue` files. Instead, we still use Webpack + vue-loader to bundle our test files. The recommended setup is using [Karma](http://karma-runner.github.io/0.13/index.html) and [karma-webpack](https://github.com/webpack/karma-webpack).
4+
5+
Karma is a test runner that launches browsers and runs your tests for you. You can choose what browsers you want to test in and what test framework (e.g. Mocha or Jasmine) you want to use. Here is an example Karma configuration that runs the tests inside [PhantomJS](http://phantomjs.org/) with the [Jasmine](http://jasmine.github.io/edge/introduction.html) test framework:
6+
7+
``` bash
8+
npm install\
9+
karma karma-webpack\
10+
karma-jasmine jasmine-core\
11+
karma-phantomjs-launcher phantomjs\
12+
--save-dev
13+
```
14+
15+
``` js
16+
// karma.conf.js
17+
module.exports = function (config) {
18+
config.set({
19+
browsers: ['PhantomJS'],
20+
frameworks: ['jasmine'],
21+
// this is the entry file for all our tests.
22+
files: ['test/index.js'],
23+
// we will pass the entry file to webpack for bundling.
24+
preprocessors: {
25+
'test/index.js': ['webpack']
26+
},
27+
// we can just use the exact same webpack config by requiring it
28+
webpack: require('./webpack.config.js'),
29+
// avoid walls of useless text
30+
webpackMiddleware: {
31+
noInfo: true
32+
},
33+
singleRun: true
34+
})
35+
}
36+
```
37+
38+
And for the entry `test/index.js` file:
39+
40+
``` js
41+
// test/index.js
42+
// require all test files using special Webpack feature
43+
// https://webpack.github.io/docs/context.html#require-context
44+
var testsContext = require.context('.', true, /\.spec$/)
45+
testsContext.keys().forEach(testsContext)
46+
```
47+
48+
This entry file simply requires all other files that ends in `.spec.js` in the same folder. Now we can actually write some tests:
49+
50+
``` js
51+
// test/component-a.spec.js
52+
var Vue = require('vue')
53+
var ComponentA = require('../../src/components/a.vue')
54+
55+
describe('a.vue', function () {
56+
57+
// asserting JavaScript options
58+
it('should have correct message', function () {
59+
expect(ComponentA.data().msg).toBe('Hello from Component A!')
60+
})
61+
62+
// asserting rendered result by actually rendering the component
63+
it('should render correct message', function () {
64+
var vm = new Vue({
65+
template: '<div><test></test></div>',
66+
components: {
67+
'test': ComponentA
68+
}
69+
}).$mount()
70+
expect(vm.$el.querySelector('h2.red').textContent).toBe('Hello from Component A!')
71+
})
72+
})
73+
```
74+
75+
To run the tests, add the following NPM script:
76+
77+
``` js
78+
// package.json
79+
...
80+
"scripts": {
81+
...
82+
"test": "karma start karma.conf.js"
83+
}
84+
...
85+
```
86+
87+
Finally, run:
88+
89+
``` bash
90+
npm test
91+
```
92+
93+
Again, [vue-loader-example](https://github.com/vuejs/vue-loader-example) contains a fully working example with tests.

0 commit comments

Comments
 (0)