-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to mock custom imports while using mountSuspended to test #935
Comments
Hey @jamesjoung! 👋 I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together! To mock custom imports while using import { it, expect, vi } from "vitest";
import { mountSuspended, mockNuxtImport } from "@nuxt/test-utils/runtime";
import App from "../../layouts/default.vue";
// Mock the custom import using mockNuxtImport
mockNuxtImport('@/service/message-service', () => {
return {
getMessage: () => "hello from mock!!",
};
});
it("nuxt unit testing", async () => {
const component = await mountSuspended(App, { route: "/" });
expect(component.html()).toMatchInlineSnapshot(
`"<div>Message: hello from mock!!</div>"`
);
}); This code demonstrates how to mock the Additionally, ensure you have set up your testing environment correctly by adding For more detailed examples and additional use cases, you can refer to the Nuxt test-utils documentation [1][2][3][4][5]. |
Environment
"dependencies": {
"nuxt": "3.12.4",
"vue": "latest"
},
"devDependencies": {
"@nuxt/test-utils": "3.14.1",
"@vue/test-utils": "2.4.6",
"happy-dom": "14.12.3",
"jest-image-snapshot": "6.4.0",
"playwright-core": "1.46.0",
"vitest": "2.0.5"
}
Reproduction
Default.vue
App.vue
App.test.ts
message-service.ts
Describe the bug
I was expecting vitest vi.mock should be able to mock imports to provide custom/mock implementation, however it seems that vi.mock is not working in the context of nuxt test?
If so, how are we suppose to create unit tests and/or E2E using nuxt/test-utils package and be able to provide flexible mocking mechanism?
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: