Closed EvandrooViegas closed 1 year ago
btw this is my jest config:
{
"preset": "ts-jest",
"transform": {
"^.+\\.svelte$": [
"svelte-jester",
{
"preprocess": true
}
],
"^.+\\.ts$": [
"ts-jest",
{
"useESM": true
}
]
},
"moduleFileExtensions": ["js", "ts", "svelte"],
"extensionsToTreatAsEsm": [".svelte", ".ts"],
"testEnvironment": "jsdom",
"moduleNameMapper": {
"\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/mocks/fileMock.js"
}
}
See the Module mocking in ESM section in the ECMAScript Modules guide.
The thing is that jest.mock()
and jest.requireActual()
will work only for CJS modules. For details on mocking ESM modules check the link above.
Thanks! It actually worked. This is the solved code if anyone needs it:
import { describe, jest } from "@jest/globals";
import { render, screen } from "@testing-library/svelte";
import Page from "./+page.svelte";
jest.unstable_mockModule("./+page.server", () => {
return {
__esModule: true,
load: jest.fn(() => Promise.resolve(({
agents: [
{ uuid: "", displayName: "a", description: "", displayIcon: "", fullPortrait: "", background: "", role: { displayName: "", displayIcon: "" }, abilities: [] },
{ uuid: "", displayName: "b", description: "", displayIcon: "", fullPortrait: "", background: "", role: { displayName: "", displayIcon: "" }, abilities: [] },
{ uuid: "", displayName: "c", description: "", displayIcon: "", fullPortrait: "", background: "", role: { displayName: "", displayIcon: "" }, abilities: [] },
],
})))
};
})
const { load } = await import("./+page.server")
describe("testing the home page", () => {
it("should load and render all agents on the page", () => {
render(Page)
expect(load).toBeCalled()
expect(screen.getAllByTestId("agent-a")).toHaveLength(1)
expect(screen.getAllByTestId("agent-b")).toHaveLength(1)
expect(screen.getAllByTestId("agent-c")).toHaveLength(1)
})
})
Glad it work.
By the way, you can skip __esModule: true
. If I remember it right, Babel is adding that prop for code transformed from ESM to CJS. That is needed for some interop (default imports, perhaps?). You don’t transform between ESM and CJS, so just skip that.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Version
29.6.3
Steps to reproduce
cd client
npm run test
Expected behavior
I expected the load function from home.page.test.ts file to be mocked properly
Actual behavior
this is the test, here I'm mocking a function return. When i try to run the test I get this:
Additional context
This project is built with sveltekit and typescript, the function that I'm trying to mock is the load function, it is responsible to provide the data to the page. What I'm trying to do at the code above is mocking the load function to return dummy data, and I'm expecting to this function be called and the items returned from this function to be rendered correctly on the page.
Environment