Encountered a TypeError: Object prototype may only be an Object or null: undefined
I ran into an issue while working on my project. I'm utilizing vuejs, typescript, and jest.
Despite having simple code, I encountered an error when trying to unit test with jest. Below is the snippet of my test code:
///<reference path="../../../../node_modules/@types/jest/index.d.ts"/>
// https://vue-test-utils.vuejs.org/kr/s
import { mount } from "vue-test-utils";
import HelloWorld from './HelloWorld.vue';
describe('[HelloWorld]', function () {
let cmp: any;
beforeEach(() => {
cmp = mount(HelloWorld);
});
it("Check vue instance", () => {
expect(cmp.isVueInstance()).toBeTruthy();
});
it("message is Hello", () => {
expect(cmp.vm.msg).toEqual('Hello!!');
});
});
And here is the content of the vue file:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<img src="/assets/logo.png">
<button @click="clickHandler">
button
</button>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
interface HelloWorldInterface {
msg: string;
clickHandler(): void;
}
@Component({})
export default class HelloWorld extends Vue implements HelloWorldInterface {
msg = "Hello!!";
clickHandler() {
window.alert(this.msg);
}
}
</script>
For details and a visual representation of the error logs, click here.