with Jest
Delightful JavaScript Testing Framework with a focus on simplicity.
Unit
Integration
E2E
Static analysis: Eslint or static
type checking (TS or Flow)
Jest
Cypress
💵
💵
💵
💵
Unit tests should focus on behaviors that are mostly pure:
Given the same inputs, always return the same output
Have no side-effects
Isolate side effects from business rules and domain logic.
- Eric Elliot
All tests must be isolated from other tests. Tests should have no shared mutable state.
Factory functions
Search Box
Input
Clear Button
Search Button
Store
Input component
Clear button component
Search button component
+ Some smoke tests 💨 ?
Utils
Factory
Use factory functions to provide (shared) setups.
Try to focus on the most important parts (no mandatory level code coverage)
Try to use "equal" comparisons instead of weird assertions (better readability).
Isolation: pure (no side effects) and controlled context
Confidence:
it('has the expected html structure', () => {
expect(wrapper.element).toMatchSnapshot()
})
Snapshot testing
it('emits correct structure to new fields', () => {
// arrange
wrapper.setProps({
fields: [
{
type: 'text',
labelledAs: 'My author',
},
],
})
// act
const position = 0
wrapper.vm.updateModel('value', 'key', position)
// assert
expect(wrapper.emitted('update')[position]).toEqual([[{ key: 'value' }]])
expect(wrapper.vm.currentModelValues).toEqual([{ key: 'value' }])
})
Testing methods, local state and emissions
demo