How to use jest.spyOn with @swc/jest? #7024
-
I'm trying to use @swc/jest on my project but when I try it I get the following error: TypeError: Cannot redefine property: deleteTemplate
at Function.defineProperty (<anonymous>) jest.spyOn(API, "deleteTemplate") Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
I found a pattern that works for a project I'm migrating to swc at work. |
Beta Was this translation helpful? Give feedback.
-
I am also stuck on this. The above solutions don't work for me because I am using a 3rd party library that uses swc, and I get this same error. My project does not use swc. jest.mock() with mockReturnedValue and mockImplementation don't use my mock value. Any suggestions are really appreciated. |
Beta Was this translation helpful? Give feedback.
-
Found by chance a nice and easy workaround that does not involve changing anything in the non-test code (at least for me) : import * as externallibrary from "external-library";
jest.mock("external-library", () => ({
__esModule: true,
...jest.requireActual("external-library"),
}));
describe("foo tests", () => {
it("should bar if blop", () => {
jest.spyOn(externallibrary, "externalMethod").mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as ReturnType<typeof externallibrary.externalMethod>);
//...
})
}) Thanks to iarroyo on StackOverflow |
Beta Was this translation helpful? Give feedback.
-
Historical context from #5059:
I used |
Beta Was this translation helpful? Give feedback.
Try this https://www.npmjs.com/package/jest_workaround