Skip to content

Commit af37800

Browse files
authored
Merge pull request #1373 from vuejs/docs/issue-1192-vue-test-utils-without-a-build-step
Guide on using vue test utils without a build step
2 parents 79eab20 + 1a96878 commit af37800

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Diff for: docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Vue Test Utils is the official unit testing utility library for Vue.js.
1010
- [Testing Single-File Components with Jest](guides/testing-single-file-components-with-jest.md)
1111
- [Testing Single-File Components with Mocha + webpack](guides/testing-single-file-components-with-mocha-webpack.md)
1212
- [Testing Single-File Components with Karma](guides/testing-single-file-components-with-karma.md)
13+
- [Testing in Node.js Without a Build Step](guides/usage-without-a-build-step-node.md)
1314
- [Testing Asynchronous Behavior](guides/testing-async-components.md)
1415
- [Using with TypeScript](guides/using-with-typescript.md)
1516
- [Using with Vue Router](guides/using-with-vue-router.md)

Diff for: docs/guides/usage-without-a-build-step-node.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Usage Without a Build Step
2+
3+
While it is common to build Vue applications using tools [Webpack](https://webpack.js.org/) to bundle the application, `vue-loader` to leverage Single File Components, and [Jest](https://jestjs.io/) to write expressive tests, it is possible to use Vue Test Utils with much less. The minimal requirements for Vue Test Utils, aside from the library itself are:
4+
5+
- Vue
6+
- vue-template-compiler
7+
- a DOM (be it [jsdom](https://github.com/jsdom/jsdom) in a Node environment, or the DOM in a real browser)
8+
9+
In this example, we will demonstrate how to write a simple test using nothing but the minimal dependencies described above. The final code can be found [here](https://github.com/lmiller1990/vue-test-utils-node-basic).
10+
11+
## Installing the Dependencies
12+
13+
We need to install some dependencies, as explained above: `npm install vue vue-template-complier jsdom jsdom-global @vue/test-utils`. No test runner or bundler is needed for this example.
14+
15+
## Requiring the Libraries
16+
17+
Now we need to require the libraries. There is a slight caveat, explained in a comment, and in depth below the snippet.
18+
19+
```js
20+
// jsdom-global must be required before vue-test-utils,
21+
// because vue-test-utils expects a DOM (real DOM, or JSDOM)
22+
// to exist.
23+
require('jsdom-global')()
24+
25+
const assert = require('assert')
26+
27+
const Vue = require('vue')
28+
const VueTestUtils = require('@vue/test-utils')
29+
```
30+
31+
As the comment says, `jsdom-global` must be required before `@vue/test-utils`. This is because Vue Test Utils expects a DOM to be present to render the Vue components. If you are running the tests in a real browser, you will not need `jsdom` at all. `Vue` must also be required before `@vue/test-utils` for obvious reasons - Vue Test Utils expects to be available, as well. We also require `assert` from the node standard library. Normally we would use the matchers provided by a test runner, often in the format of an `expect(...).toEqual(...)` assertion, but `assert` will serve this purpose for this example.
32+
33+
## Writing a Test
34+
35+
Now everything is set up, all we need is a component and a test. To keep things simple, we will just render some text and assert it is present in the rendered component.
36+
37+
```js
38+
const App = Vue.component('app', {
39+
data() {
40+
return {
41+
msg: 'Hello Vue Test Utils'
42+
}
43+
},
44+
45+
template: `
46+
<div>{{ msg }}</div>
47+
`
48+
})
49+
50+
const wrapper = VueTestUtils.shallowMount(App)
51+
52+
assert.strictEqual('Hello Vue Test Utils', wrapper.text())
53+
```
54+
55+
It's as simple as it looks. Since we do not have a build step, we cannot use Single File Components. There is nothing to stop us using Vue in the same style you would when including it from a CDN via a `<script>` tag, however.

0 commit comments

Comments
 (0)