Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, but could be an individual method.
When testing in Vue, generally a unit is a component.
βWikipedia
https://github.com/phanan/counter-meetup
shallow()
, child components are stubbedimport { shallow } from '@vue/test-utils'
import Counter from '@/components/Counter'
describe('Counter.vue', () => {
it('renders', () => {
const wrapper = shallow(Counter)
expect(wrapper.contains('.result')).toBe(true)
})
})
Assert a component's public interface, not its implementation details
wrapper.vm.inc()
// this won't work anyway
expect(wrapper.find('.result').text()).toBe('1')
wrapper.find('button').trigger('click')
expect(wrapper.find('.result').text()).toBe('1')
propsData
or
setProps()
setData()
renderToString(Foo)
, which uses vue-server-renderer
import Counter from '@/components/Counter'
describe('Counter.vue', () => {
it('renders', () => {
expect(shallow(Counter).contains('.result', 'button')).toBe(true)
}
it('counts', () => {
expect(shallow(Counter).click('button').find('.result').text()).toBe('1')
}
}
github.com/phanan
twitter.com/notphanan
phanan.net