Fixtures
Let's learn how to use the fixtures in Cypress testing.
We'll cover the following...
Introduction
Let’s take a look at the following test:
Press + to interact
import { paths } from "/educative-cypress-course/realworld/frontend/src/components/App";import { noArticles } from "/educative-cypress-course/realworld/frontend/src/components/ArticleList";import { strings } from "/educative-cypress-course/realworld/frontend/src/components/Register";const headers = { "Access-Control-Allow-Origin": "*" }context("Signup flow", () => {it("The happy path should work", () => {const user = {username: "Tester",email: "user@realworld.io",password: "mysupersecretpassword"};// set up AJAX call interceptioncy.intercept("POST", "**/api/users", {body: {user: {username: "Tester",email: "user@realworld.io",token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVkN2ZhZjc4YTkzNGFiMDRhZjRhMzE0MCIsInVzZXJuYW1lIjoidGVzdGVyNzk1MzYiLCJleHAiOjE1NzM4MzY2ODAsImlhdCI6MTU2ODY0OTA4MH0.zcHxMz2Vx5h-EoiUZlRyUw0z_A_6AIZ0LzQgROvsPqw"}},headers}).as("signup-request");cy.intercept("GET", "**/api/tags", { body: { tags: []}, headers }).as("tags");cy.intercept("GET", "**/api/articles/feed**", { body: { articles: [], articlesCount: 0 }, headers }).as("feed");cy.visit(paths.register);// form fillingcy.findByPlaceholderText(strings.username).type(user.username)cy.findByPlaceholderText(strings.email).type(user.email)cy.findByPlaceholderText(strings.password).type(user.password);// form submit...cy.get("form").within(() => cy.findByText(strings.signUp).click())// ... and AJAX call waitingcy.wait("@signup-request").should(interception =>expect(interception.request.body).deep.equal({user: {username: user.username,email: user.email,password: user.password}})).wait(["@tags", "@feed"]);// end of the flowcy.findByText(noArticles).should("be.visible");});});
In the above test, every back-end stub has the response set ...