So I'm doing a little bit of experimental programming to ensure I understand how certain things work, and I'm coming across an error which I don't understand. I can't see any obvious solutions for this problem using Google or on here specifically.
I'm trying to confirm to myself exactly how jest.spyOn()
works when applied to module imports. But right now the answer is that it just doesn't.
spy.js:
export const foo = () => {
console.log("called foo");
};
export const bar = () => {
console.log("called bar");
};
spy.spec.js:
import * as Foo from "./spy";
import { jest } from "@jest/globals";
test("call foo", () => {
jest.spyOn(Foo, "bar");
});
package.json:
{
"type": "module",
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"jest": "^27.0.1"
}
}
When I run this test, I get the following error: TypeError: object.hasOwnProperty is not a function
. I'm running node v15.12.0.
Can somebody explain to my
- Why this error occurs
- What I can do to remove it
Edit
It has been suggested that the problem is to do with the module import lacking a prototype
property. This is not the issue - see below
import * as Foo from "./spy";
import { jest } from "@jest/globals";
test("SpyOn plain object", () => {
const bar = { baz: () => {} };
expect(bar.prototype).toBeUndefined(); // Pass
jest.spyOn(bar, "baz"); // No error
});
test("SpyOn import", () => {
expect(Foo.prototype).toBeUndefined(); // Pass
jest.spyOn(Foo, "foo"); // Error!
});