You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vuex supports hot-reloading actions and mutations during development, using Webpack's [Hot Module Replacement API](https://webpack.github.io/docs/hot-module-replacement.html). You can also use it in Browserify with the [browserify-hmr](https://github.com/AgentME/browserify-hmr/) plugin.
3
+
Vuex supports hot-reloading mutations, modules, actions and getters during development, using Webpack's [Hot Module Replacement API](https://webpack.github.io/docs/hot-module-replacement.html). You can also use it in Browserify with the [browserify-hmr](https://github.com/AgentME/browserify-hmr/) plugin.
4
4
5
-
It's as simple as calling `store.hotUpdate()`with the new actions and mutations:
5
+
For mutations and modules, you need to use the `store.hotUpdate()`API method:
// have to add .default here due to babel 6 module output
20
-
constnewActions=require('./actions').default
21
31
constnewMutations=require('./mutations').default
32
+
constnewModuleA=require('./modules/a').default
22
33
// swap in the new actions and mutations
23
34
store.hotUpdate({
24
-
actions: newActions,
25
-
mutations: newMutations
35
+
mutations: newMutations,
36
+
modules: {
37
+
a: newModuleA
38
+
}
26
39
})
27
40
})
28
41
}
29
42
```
43
+
44
+
You don't need to do anything specific for actions and getters. Webpack's hot module replacement system will "bubble" changes up the dependency chain - and changes in actions and getters will bubble up to the Vue components that imported them. Because Vue components loaded via `vue-loader` are automatically hot-reloadable, these affected components will hot-reload themselves and use the updated actions and getters.
Copy file name to clipboardExpand all lines: docs/en/testing.md
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,22 @@
1
1
# Testing
2
2
3
-
Mutations are very straightforward to test, because they are just functions that completely rely on their arguments. Actions can be a bit more tricky because they may call out to external APIs. When testing actions, we usually need to do some level of mocking - for example, we can abstract the API calls into a service and mock that service inside our tests. In order to easily mock dependencies, we can use Webpack and [inject-loader](https://github.com/plasticine/inject-loader) to bundle our test files.
3
+
The main parts we want to unit test in Vuex are mutations and actions.
4
4
5
-
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
5
+
### Testing Mutations
6
+
7
+
Mutations are very straightforward to test, because they are just functions that completely rely on their arguments. One trick is that if you are using ES2015 modules and put your mutations inside your `store.js` file, in addition to the default export, you can also export the mutations as a named export:
8
+
9
+
```js
10
+
conststate= { ... }
11
+
12
+
// export mutations as a named export
13
+
exportconstmutations= { ... }
14
+
15
+
exportdefaultnewVuex.Store({
16
+
state,
17
+
mutations
18
+
})
19
+
```
6
20
7
21
Example testing a mutation using Mocha + Chai (you can use any framework/assertion libraries you like):
Actions can be a bit more tricky because they may call out to external APIs. When testing actions, we usually need to do some level of mocking - for example, we can abstract the API calls into a service and mock that service inside our tests. In order to easily mock dependencies, we can use Webpack and [inject-loader](https://github.com/plasticine/inject-loader) to bundle our test files.
51
+
31
52
Example testing an async action:
32
53
33
54
```js
@@ -97,7 +118,11 @@ describe('actions', () => {
97
118
})
98
119
```
99
120
100
-
### Running in Node
121
+
### Running Tests
122
+
123
+
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
124
+
125
+
#### Running in Node
101
126
102
127
Create the following webpack config:
103
128
@@ -130,9 +155,13 @@ webpack
130
155
mocha test-bundle.js
131
156
```
132
157
133
-
### Running in Browser
158
+
####Running in Browser
134
159
135
160
1. Install `mocha-loader`
136
-
2. Change the `entry` from the Webpack config above to `'mocha!babel!./test.js'`.
161
+
2. Change the `entry` from the Webpack config above to `'mocha!babel!./test.js'`.
137
162
3. Start `webpack-dev-server` using the config
138
163
4. Go to `localhost:8080/webpack-dev-server/test-bundle`.
164
+
165
+
#### Running in Browser with Karma + karma-webpack
166
+
167
+
Consult the setup in [vue-loader documentation](http://vuejs.github.io/vue-loader/workflow/testing.html).
0 commit comments