-
Notifications
You must be signed in to change notification settings - Fork 665
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
JessicaSachs
merged 5 commits into
dev
from
docs/issue-1192-vue-test-utils-without-a-build-step
Dec 18, 2019
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46583c8
(docs): add a guide on testing without a build step in node.js
lmiller1990 ddffd8a
(docs): fix linting in docs
lmiller1990 47de198
(docs): run formatter
lmiller1990 18e279a
(docs): refer to Vue Test Utils correctly
lmiller1990 1a96878
(docs): update webpack link
lmiller1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://jestjs.io/) 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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.