Skip to content

Instantly share code, notes, and snippets.

@eddyerburgh
Last active April 8, 2022 11:05
Show Gist options
  • Save eddyerburgh/918af21eb465eba04b8401f2063124f8 to your computer and use it in GitHub Desktop.
Save eddyerburgh/918af21eb465eba04b8401f2063124f8 to your computer and use it in GitHub Desktop.
vue-test-utils hasClass example
import { mount } from 'vue-test-utils'
import Component from '@/components/ComponentToTest'
const wrapper = mount(ComponentToTest)
// Traversal
const Foo = wrapper.find(Foo)
const divs = wrapper.findAll('div')
const div = divs.at(0)
// Actions
Foo.setData({ foo: true })
Foo.setProps({ bar: true })
div.trigger('click')
// Assertions
expect(Foo.hasProp('bar', 'foobar')).toBe(true)
expect(div.hasClass('foo')).toBe(true)
expect(div.hasStyle('color', 'red')).toBe(true)
expect(div.is('.foo')).toBe(true)
@rydurham
Copy link

rydurham commented Jun 1, 2018

It appears that hasClass() will be deprecated in test-utils v1. The new prefered method is to use "classes()" instead, like so:

expect(div.classes()).toContain('foo')

hasStyle() will also be deprecated:

expect(div.element.styles.color).toBe('red')

@uthpalax
Copy link

uthpalax commented Jun 3, 2018

How to check if a class does not exist in element

something like
expect(div.hasClass('foo')).toBe(false)

how to do it using classes()

@mukulkhanna
Copy link

mukulkhanna commented Jun 17, 2018

@Uthpala

expect(div.classes()).not.toContain('foo')

@niczak
Copy link

niczak commented Feb 17, 2021

@Uthpala

expect(div.classes()).not.toContain('foo')

This is an oldie but a goodie, thank you @Uthpala!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment