0

When I run the Cypress test with my React app (created using npx) I am getting the error:

Property 'to' does not exist on type 'JestMatchers<JQuery<HTMLElement>>'.ts(2339).

How can I fix it?

Here is my Cypress test file:

 import axios from "axios";

describe("Bookish application", function () {
  before(() => {
    return axios.delete("http://localhost:8080/books?_cleanup=true").catch((err) => err);
  });

  afterEach(() => {
    return axios.delete("http://localhost:8080/books?_cleanup=true").catch((err) => err);
  });

  beforeEach(() => {
    const books = [
      { name: "Refactoring", id: 1 },
      { name: "Domain-driven design", id: 2 },
      { name: "Building Microservices", id: 3 },
    ];
    return books.map((item) => axios.post("http://localhost:8080/books", item, { headers: { "Content-Type": "application/json" } }));
  });

  it("Visits the bookish", function () {
    cy.visit("http://localhost:3000/");
    cy.get('h2[data-test="heading"]').contains("Bookish");
  });

  it("Shows a book list", () => {
    cy.visit("http://localhost:3000/");
    cy.get('div[data-test="book-list"]').should("exist");
    cy.get("div.book-item").should((books) => {
      expect(books).to.have.length(3); //error
      const titles = [...books].map((x) => x.querySelector("h2")?.innerHTML);
      expect(titles).to.deep.equal(["Refactoring", "Domain-driven design", "Building Microservices"]);
    });
  });
});

package.json:

    {
  "name": "maduk-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@emotion/react": "^11.11.1",
    "@emotion/styled": "^11.11.0",
    "@mui/icons-material": "^5.14.3",
    "@mui/material": "^5.14.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/node": "^16.18.40",
    "@types/react": "^18.2.20",
    "@types/react-dom": "^18.2.7",
    "@types/testing-library__jest-dom": "^5.14.3",
    "axios": "^1.4.0",
    "cypress": "^12.17.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "ts-node": "^10.9.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "e2e": "cypress run",
    "stub-server": "npx json-server --watch -p 8080 stub-server/db.json"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
    "@testing-library/jest-dom": "^6.0.1",
    "@types/jest": "^29.5.3",
    "jest": "^29.6.2",
    "json-server": "^0.17.3",
    "ts-jest": "^29.1.1",
    "typescript": "^5.1.6"
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

0 Answers0