Migrate Cypress E2E from cy.intercept to MSW service worker
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -40,23 +40,29 @@ describe("Login Flow", () => {
|
||||
cy.get("#password").type("password123");
|
||||
cy.get("#login_btn_main_btn").click();
|
||||
|
||||
cy.wait("@postToken");
|
||||
cy.url().should("include", "/files");
|
||||
});
|
||||
|
||||
it("failed login shows error message", () => {
|
||||
// Override the token intercept to return 401
|
||||
cy.intercept("POST", "/api/oauth/token", {
|
||||
statusCode: 401,
|
||||
body: { detail: "Incorrect username or password" },
|
||||
}).as("postTokenFail");
|
||||
|
||||
// Visit login first to load app + MSW
|
||||
cy.visit("/login");
|
||||
cy.get("#login_btn_main_btn").should("exist");
|
||||
// Override the token endpoint to return 401 via MSW
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.post("/api/oauth/token", () =>
|
||||
HttpResponse.json(
|
||||
{ detail: "Incorrect username or password" },
|
||||
{ status: 401 },
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
cy.get("#account").type("wronguser");
|
||||
cy.get("#password").type("wrongpass");
|
||||
cy.get("#login_btn_main_btn").click();
|
||||
|
||||
cy.wait("@postTokenFail");
|
||||
cy.contains("Incorrect account or password").should("be.visible");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user