This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
verbose: true, | |
moduleFileExtensions: [ | |
"js", | |
"json", | |
"vue" | |
], | |
transform: { | |
".*\\.(vue)$": "vue-jest", | |
"^.+\\.js$": "<rootDir>/node_modules/babel-jest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { shallowMount } from '@vue/test-utils'; | |
import Component from './component'; // name of your Vue component | |
let wrapper; | |
beforeEach(() => { | |
wrapper = shallowMount(Component, { | |
propsData: {}, | |
mocks: {}, | |
stubs: {}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": [ | |
["env", { "modules": false }] | |
], | |
"env": { | |
"test": { | |
"presets": [ | |
["env", { "targets": { "node": "current" }}] | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { shallowMount, createLocalVue } from '@vue/test-utils'; | |
import Vuex from 'vuex'; | |
import Component from './component'; | |
let wrapper; | |
let store; | |
let actions; | |
let mutations; | |
let state; | |
const localVue = createLocalVue(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('has blah class', () => { | |
expect(wrapper.contains('.blah')).toBe(true); | |
}); | |
// check multiple elements by verifying count | |
test('has blah classes', () => { | |
expect(wrapper.findAll('.blah').length).toBe(10); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('emit events when close-btn clicked', () => { | |
const closeBtn = wrapper.find('.close-btn'); | |
closeBtn.trigger('click'); | |
expect(wrapper.emitted().close.length).toBe(1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let wrapper; | |
const factory = (computed = {}) => { | |
return shallowMount(Component, { | |
propsData: {}, | |
mocks: {}, | |
stubs: {}, | |
methods: {}, | |
computed, | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using jest.fn() to mock action and mutation functions | |
beforeEach(() => { | |
actions = { | |
someAction: jest.fn(() => true) | |
}; | |
mutations = { | |
someMutation: jest.fn(() => false) | |
}; | |
state = { | |
key: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// used in component | |
<script> | |
import Constants from '../constants'; | |
console.log(Constants.myConstant); | |
</script> | |
// in test file | |
jest.mock('../constants', () => ({ | |
'myConstant': 'myValue', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('check async block', async() => { | |
await wrapper.vm.asyncFunction(); // where asyncFunction() has a resolved Promise or other async stuff | |
}); |
OlderNewer