Skip to content

Guide on using vue test utils without a build step #1373

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

Merged
merged 5 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Vue Test Utils is the official unit testing utility library for Vue.js.
- [Testing Single-File Components with Jest](guides/testing-single-file-components-with-jest.md)
- [Testing Single-File Components with Mocha + webpack](guides/testing-single-file-components-with-mocha-webpack.md)
- [Testing Single-File Components with Karma](guides/testing-single-file-components-with-karma.md)
- [Testing in Node.js Without a Build Step](guides/usage-without-a-build-step-node.md)
- [Testing Asynchronous Behavior](guides/testing-async-components.md)
- [Using with TypeScript](guides/using-with-typescript.md)
- [Using with Vue Router](guides/using-with-vue-router.md)
Expand Down
55 changes: 55 additions & 0 deletions docs/guides/usage-without-a-build-step-node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Usage Without a Build Step

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:

- Vue
- vue-template-compiler
- a DOM (be it [jsdom](https://github.com/jsdom/jsdom) in a Node environment, or the DOM in a real browser)

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).

## Installing the Dependencies

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.

## Requiring the Libraries

Now we need to require the libraries. There is a slight caveat, explained in a comment, and in depth below the snippet.

```js
// jsdom-global must be required before vue-test-utils,
// because vue-test-utils expects a DOM (real DOM, or JSDOM)
// to exist.
require('jsdom-global')()

const assert = require('assert')

const Vue = require('vue')
const VueTestUtils = require('@vue/test-utils')
```

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.

## Writing a Test

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.

```js
const App = Vue.component('app', {
data() {
return {
msg: 'Hello Vue Test Utils'
}
},

template: `
<div>{{ msg }}</div>
`
})

const wrapper = VueTestUtils.shallowMount(App)

assert.strictEqual('Hello Vue Test Utils', wrapper.text())
```

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.