Replies: 4 comments 1 reply
-
|
Hey there, @GazEdge! I'm a bot designed to help you out with bugs, questions, and becoming a contributor. Feel free to ask me anything while waiting for a human maintainer to assist you. To mock the responses from external APIs for end-to-end (E2E) testing in a Nuxt application, you can use the
By using For more detailed information on setting up and using |
Beta Was this translation helpful? Give feedback.
-
|
@dosu your answer wont work, as registerEndpoint can only be used in runtime testing. Find a solution that works with @nuxt/test-utils/e2e |
Beta Was this translation helpful? Give feedback.
-
|
You can use something like mswjs, but it has issues with mocking the SSR-side of things. |
Beta Was this translation helpful? Give feedback.
-
|
I felt this pain. I've just created a tool that solves this problem https://docs.mockybalboa.com/. There's first class support for Nuxt as well as Playwright and Cypress. It allows you to write your fixtures declaratively within your tests without having to modify any application logic. No proxy servers, no static fixtures, and it supports running tests in parallel. Fixtures are scoped to the client, which means no risk of leaking fixtures outside the tests they were written for. Here's the example from the Playwright docs on how the mocks are written in the tests. The client guide has more complete documentation on how you can mock requests. It's very similar to and was inspired by the Playwright route API for client side mocking. import { test, expect } from "@playwright/test";
import { createClient } from "@mocky-balboa/playwright";
test("my page loads", async ({ page, context }) => {
// Create our Mocky Balboa client and establish a connection with the server
const client = await createClient(context);
// Register our fixture on routes matching '**/api/users'
client.route("**/api/users", (route) => {
return route.fulfill({
status: 200,
body: JSON.stringify([
{ id: "user-1", name: "John Doe" },
{ id: "user-2", name: "Jane Doe" }
]),
headers: {
"Content-Type": "application/json"
},
});
});
// Visit the page of our application in the browser
await page.goto("http://localhost:3000");
// Our mock above should have been returned on our server
await expect(page.getByText("John Doe")).toBeVisible();
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Im using e2e testing, but rather than use /server/api for my endpoints (called client side only), I use external apis.
How can i mock the responses from external apis?
Ive tried a few 3rd party tools, like miragejs, but im guessing its not being pulled into the nuxt test instance.
Does anyone have a solution to this? I know i could probably use runtime and component testing to apply mocks to the api calls, but I'd prefer to run tests e2e, but with mocked external apis.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions