I have a Vue2 project with Vuetify, and i am using Jest for unit testing my code. I am starting out testing some sample code and i simple cannot get Jest to determine if a Vuetify v-alert
component is visible or not. I have tried the built in Jest methods as well as adding Jest-dom and using the toBeVisible()
method and nothing is working so far.
If you look at the Test.vue component, the v-alert
component is hidden by default.(Its style is set to display: none;)
The unit test says expect(alert).not.toBeVisible()
which should pass, but it always fails regardless of what the v-alert
model is set to. If i change the test to expect(alert).toBeVisible()
it passes regardless of the v-alert
model is set to true/false.
If i change the test to be expect(alert).toHaveStyle({ display: 'none' });
it fails regardless of if i have the model set to true/false.
So as far as i can tell the Jest unit test CANNOT determine the visibility of the v-alert
component at all. These same test work fine on the v-btn
component just fine so why does the v-alert
break? This is just my first unit test sample that ive been trying to get working for 2 days now. I have an entire application to write tests for and so far Jest is not working very well with Vuetify...any suggestions?
// Test.vue component
<template>
<div>
<v-btn ref="btn" depressed tile @click="showAlert">Show Alert</v-btn>
<v-alert
v-model="showError"
ref="error-msg"
type="error"
transition="scale-transition"
width="410"
tile
dense
dismissible
@input="clearError"
>
{{ errorText }}
</v-alert>
</div>
</template>
<script>
export default {
data() {
return {
showError: false,
errorText: ''
};
},
methods: {
showAlert() {
this.errorText = 'Test Error message';
this.showError = true;
},
clearError() {
this.errorText = '';
}
}
};
</script>
// Jest Unit test
// Libraries
import Vue from 'vue';
import Vuetify from 'vuetify';
// Components
import Test from '@/components/Login/Test.vue';
// Utilities
import { createLocalVue, shallowMount } from '@vue/test-utils';
// Import Jest Dom test utils.
import '@testing-library/jest-dom';
const localVue = createLocalVue();
Vue.use(Vuetify);
describe('Test Page', () => {
let vuetify;
beforeEach(() => {
vuetify = new Vuetify();
});
it('Check visibility of button', () => {
const wrapper = shallowMount(Test, {
localVue,
vuetify
});
const btn = wrapper.findComponent({ ref: 'btn' }).element;
expect(btn).toBeVisible();
});
it('Error Message hidden on page load', () => {
const wrapper = shallowMount(Test, {
localVue,
vuetify
});
const alert = wrapper.findComponent({ ref: 'error-msg' }).element;
expect(alert).not.toBeVisible();
});
});
// Package.json
"dependencies": {
"vue": "^2.6.11",
"vue-click-outside": "^1.1.0",
"vue-debounce": "^2.5.7",
"vue-router": "^3.3.4",
"vuetify": "^2.2.11",
"vuex": "^3.4.0"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "^7.10.3",
"@babel/polyfill": "^7.10.1",
"@fortawesome/fontawesome-free": "^5.13.1",
"@testing-library/jest-dom": "^5.10.1",
"@vue/cli-plugin-babel": "^4.4.5",
"@vue/cli-plugin-e2e-nightwatch": "^4.4.5",
"@vue/cli-plugin-eslint": "^4.4.5",
"@vue/cli-plugin-unit-jest": "^4.4.5",
"@vue/cli-service": "^4.4.5",
"@vue/eslint-config-prettier": "^4.0.1",
"@vue/test-utils": "^1.0.3",
"babel-eslint": "^10.0.3",
"babel-jest": "^26.1.0",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^6.2.2",
"node-sass": "^4.14.1",
"sass": "^1.26.9",
"sass-loader": "^8.0.2",
"vue-cli-plugin-vuetify": "^2.0.6",
"vue-template-compiler": "^2.6.11",
"vuetify-loader": "^1.5.0"
}