Skip to content

Instantly share code, notes, and snippets.

View achhunna's full-sized avatar

Achhunna M. achhunna

View GitHub Profile
@achhunna
achhunna / jest.config.js
Created May 4, 2019 19:30
Jest config file
module.exports = {
verbose: true,
moduleFileExtensions: [
"js",
"json",
"vue"
],
transform: {
".*\\.(vue)$": "vue-jest",
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
@achhunna
achhunna / component.test.js
Last active May 5, 2019 01:59
Jest test template
import { shallowMount } from '@vue/test-utils';
import Component from './component'; // name of your Vue component
let wrapper;
beforeEach(() => {
wrapper = shallowMount(Component, {
propsData: {},
mocks: {},
stubs: {},
@achhunna
achhunna / .babelrc
Created May 5, 2019 00:17
Babelrc file
{
"presets": [
["env", { "modules": false }]
],
"env": {
"test": {
"presets": [
["env", { "targets": { "node": "current" }}]
]
}
@achhunna
achhunna / component-vuex.test.js
Last active May 5, 2019 01:58
Jest test template with Vuex
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();
@achhunna
achhunna / component.test.js
Created May 10, 2019 16:54
Existence of DOM elements
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);
});
@achhunna
achhunna / component.test.js
Created May 12, 2019 05:22
DOM action events
test('emit events when close-btn clicked', () => {
const closeBtn = wrapper.find('.close-btn');
closeBtn.trigger('click');
expect(wrapper.emitted().close.length).toBe(1);
});
@achhunna
achhunna / component.test.js
Last active April 22, 2020 17:09
Factory function to mount component
let wrapper;
const factory = (computed = {}) => {
return shallowMount(Component, {
propsData: {},
mocks: {},
stubs: {},
methods: {},
computed,
});
};
@achhunna
achhunna / component.test.js
Last active May 19, 2019 18:05
Mocking methods
// using jest.fn() to mock action and mutation functions
beforeEach(() => {
actions = {
someAction: jest.fn(() => true)
};
mutations = {
someMutation: jest.fn(() => false)
};
state = {
key: {}
@achhunna
achhunna / component.test.js
Created May 19, 2019 21:27
Mocking modules
// used in component
<script>
import Constants from '../constants';
console.log(Constants.myConstant);
</script>
// in test file
jest.mock('../constants', () => ({
'myConstant': 'myValue',
@achhunna
achhunna / component.test.js
Created May 19, 2019 22:06
Async callbacks
test('check async block', async() => {
await wrapper.vm.asyncFunction(); // where asyncFunction() has a resolved Promise or other async stuff
});