vue-test includes a chai plugin which you can use to test a component mounted
with the mount()
function.
It cannot test Vue components directly as it relies on the utility functions
provided by mount()
. Wrap your components using mount()
first: the
documentation for that can be found in mount-api.md.
Here's how you add the plugin to chai:
import { chaiPlugin } from 'vue-test';
chai.use(chaiPlugin);
This adds a number of useful functions that you can use when using the BDD
style of chai assertions with expect
and should
.
Here's a very quick overview of what you can do:
expect(mountedComponent).to.be.ok
expect(mountedComponent).to.be.tag('p')
expect(mountedComponent).to.contain.tag('p')
expect(mountedComponent).to.match.selector('#id .class')
expect(mountedComponent).to.contain.selector('#id .class')
expect(mountedComponent).to.be.empty
expect(mountedComponent).to.have.className('alert')
expect(mountedComponent).to.not.be.fragment
expect(mountedComponent).to.have.value('input value')
expect(mountedComponent).to.have.text('some text')
expect(mountedComponent).to.contain.text('some text')
expect(mountedComponent).to.have.attribute('style')
expect(mountedComponent).to.have.attribute('style').that.equals('something')
All of these are negatable: just add .not
after .to
.
And a full breakdown:
Tests whether a component exists.
expect(mountedComponent).to.be.ok; // correct
expect(mountedComponent.find('.not-found')).to.be.ok; // will throw
Tests whether a component is an element with a given tag name.
expect(mountedComponent).to.be.tag('p');
expect(mountedComponent).not.to.be.tag('div');
Tests whether a component contains an element with a given tag name.
expect(mountedComponent).to.contain.tag('p');
Tests whether a component matches a given selector.
expect(mountedComponent).to.match.selector('p.text [data-something]');
Tests whether a component contains an element matching a given selector.
expect(mountedComponent).to.contain.selector('.component__child');
Tests whether a component has no children.
expect(mountedComponent).to.be.empty;
Tests whether a component has a given class name.
expect(mountedComponent).to.have.className('component--red');
Tests whether a component has a given value (useful for input elements).
expect(mountedComponent).to.have.value('input text here');
Tests whether a component's text equals a given value.
expect(mountedComponent).to.have.text('Hello world!');
Tests whether a component's text contains a given value.
expect(mountedComponent).to.contain.text('world!');
Tests whether a component has a given attribute.
expect(mountedComponent).to.have.attribute('id');
Tests whether a component has a given attribute, and tests it against a given value.
expect(mountedComponent).to.have.attribute('id').that.equals('app');