I'm trying to test the methods in jest framework. For javascript methods i was able to import the methods from file using require() function and test it (expect(addition(1,2).toBe(3))). But unable to replicate the same in VueJS methods.
// VueJS
export default defineComponent({
name: "App",
components: {
HelloWorld,
},
methods: {
addition(a: number, b: number) {
return a + b;
},
subtraction(a: number, b: number) {
return a - b;
},
multiplication(a: number, b: number) {
return a * b;
},
},
});
test file
import addition from "./App.vue"
describe("HelloWorld.vue", () => {
it("testing vue method", () => {
expect(addition(1,2).toBe(3));
});
});
//error on line 4 states "Value of type 'DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin,
// ComponentOptionsMixin, ... 4 more ..., {}>' is not callable. Did you mean to include 'new'?"
additionfunction for some reason. It's the same with vanilla JS tests when imports go wrong. Could beexpect(comp.options.methods.addition.call(mockInstance, 1, 2)).... But it's correct to test a component with Vue utils as said. A test with Jest alone doesn't have much value because you test not original behaviour but something else.