Skip to content
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

Adding Plugins Guide #15

Merged
merged 8 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn-error.log*

.idea
3 changes: 2 additions & 1 deletion src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const sidebar = {
children: [
'/guide/installation',
'/guide/introduction',
'/guide/a-crash-course'
'/guide/a-crash-course',
'/guide/plugins'
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion src/guide/a-crash-course.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# A Crash Course

Let's jump write into it, and learn Vue Test Utils (VTU) by building a simple Todo app, and writing tests as we go. This guide will cover:
Let's jump right into it, and learn Vue Test Utils (VTU) by building a simple Todo app, and writing tests as we go. This
guide will cover:

- mounting components
- finding elements
Expand Down
84 changes: 84 additions & 0 deletions src/guide/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
## Plugins

Plugins add global-level functionality to Vue Test Utils' API. This is the
official way to extend Vue Test Utils' API with custom logic, methods, or
functionality.

If you're missing a bit of functionality, consider writing a plugin to
extend Vue Test Utils' API.

Some use cases for plugins:
1. Aliasing existing public methods
1. Attaching matchers to the Wrapper instance
afontcu marked this conversation as resolved.
Show resolved Hide resolved
1. Attaching functionality to the Wrapper

## Using a plugin

Install plugins by calling the `config.plugins.VueWrapper.install()` method
. This has to be done before you call `mount`.

The `install()` method will receive an instance of `WrapperAPI` containing both
public and private properties of the instance.
JessicaSachs marked this conversation as resolved.
Show resolved Hide resolved

```js
// setup.js file
import { config } from '@vue/test-utils'

// locally defined plugin, see "Writing a Plugin"
import MyPlugin from './myPlugin'

// Install a plugin onto VueWrapper
config.plugins.VueWrapper.install(MyPlugin)
```

You can optionally pass in some options:
```js
config.plugins.VueWrapper.install(MyPlugin, { someOption: true })
JessicaSachs marked this conversation as resolved.
Show resolved Hide resolved
```

Your plugin should be installed once. In Jest, this should be in your Jest
config's `setupFiles` or `setupFilesAfterEnv` file.

Some plugins automatically call `config.plugins.VueWrapper.install()` when
they're imported. This is common if they're extending multiple interfaces at
once. Follow the instructions of the plugin you're installing.

Check out the [Vue Community Guide](https://vue-community.org/guide/ecosystem
/testing.html) or [awesome-vue](https://github.com/vuejs/awesome-vue#test
) for a collection of community-contributed plugins and libraries.

## Writing a Plugin

A Vue Test Utils plugin is simply a function that receives the mounted
`VueWrapper` or `DomWrapper` instance and can modify it.
JessicaSachs marked this conversation as resolved.
Show resolved Hide resolved

Below is a simple plugin to add a convenient alias to map `wrapper.element` to `wrapper.$el`

```js
// setup.js
import { config } from '@vue/test-utils'

const myAliasPlugin = (wrapper) => {
return {
$el: wrapper.element // simple aliases
}
}

// Call install on the type you want to extend
// You can write a plugin for any value inside of config.plugins
config.plugins.VueWrapper.install(myAliasPlugin)
```

And in your spec, you'll be able to use your plugin after `mount`.
```js
// component.spec.js
const wrapper = mount({ template: `<h1>🔌 Plugin</h1>` })
console.log(wrapper.$el.innerHTML) // 🔌 Plugin
```

## Featuring your Plugin

If you're missing functionality, consider writing a plugin to extend Vue Test
Utils and submit it to be featured at [Vue Community Guide](https://vue
-community.org/guide/ecosystem/testing.html) or [awesome-vue](https
://github.com/vuejs/awesome-vue#test)