|
1 | 1 | # Testing
|
2 | 2 |
|
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