Search code examples
testingmockingxmlhttprequeste2e-testingtestcafe

RequestMock returns 222 status code with no body


I'm new to testcafe and I have tried to use RequestMock to mock a server when running the SPA tests, unfortunately, every time I run the test and I inspect the network request to the login page, I get a 222 response code so I'm not sure what I'm doing wrong.

Tried to add the mock variable to the test with requestHooks with no success either

import { Selector, RequestMock } from "testcafe";

const mock = RequestMock()
  .onRequestTo("http://web.restaurant.docker/api/login")
  .respond(
    {
      result: true,
      name: "Test Restaurant",
      data: {
        name: "Test User",
        api_token:
          "SOME_TOKEN",
        customer_id: 1
      },
      pos: { "1": "Caja", "2": "Caja Emergencias" }
    },
    200,
    {
      "access-control-allow-credentials": true,
      "access-control-allow-origin": "*"
    }
  );

fixture`Login`.page`../index.html`;

test("I can log in to the app", async t => {
  // Realiza el login
  await t
    .expect(
      Selector("form.login-form", {
        visibilityCheck: true
      }).exists
    )
    .ok()
    .typeText("#login-email", "[email protected]")
    .typeText("#login-password", "test")
    .click(".login-form button");

  // Define un nombre al pos
  await t.typeText(".dialog input", "test").click(".dialog button.ok");
  // Verifica que estoy en la ventana de seleccionar POS
  await t.expect(Selector(".restaurant-name").innerText).eql("Test Restaurant");
});

I expect to see a 200 response with the object passed to the RequestMock


Solution

  • You need to attach your RequestMock to your test or fixture as it is described here: Attaching Hooks to Tests and Fixtures.