Reputation: 4967
I'm using Jest spyOn
to track a method on Vue router. The Vue method I'm testing looks like this:
myMethod() {
this.$router.push({ name: "login" });
}
I want to ensure that $router.push
was called, so in my test I have this:
const spy = jest.spyOn(wrapper.vm.$router, 'push');
expect(spy).toHaveBeenCalledWith({ name: "login" });
This test fails, though, with the error:
Expected mock function to have been called with:
{"name": "login"}
as argument 1, but it was called with
{"name": "login", "params": {}, "path": "/login"}.
No problem, I can update the test to the following and it works:
expect(spy).toHaveBeenCalledWith({
name: "login",
params: {},
path: "/login"
});
My question is: why does the Jest spy report that it was called with an object with three properties, but when we look at the actual method call, it only has one property?
Upvotes: 2
Views: 2769
Reputation: 8340
vue-router
add some default values to params
and path
properties. When you use spyOn
you will detect the other parameters also which are assigned by vue-router
. If you add explicit the spy
to the push
method then you will see only the name
property.
Try this:
const spy = jest.fn();
wrapper.vm.$router.push = spy;
expect(spy).toHaveBeenCalledWith({ name: "login" });
Upvotes: 1