最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Property 'toMatchSnapshot' does not exist on type 'Assertion' - Stack Overflow

matteradmin5PV0评论

I am using CRA to build a web app that also uses Typescript, and Jest for (snapshot) testing. I am randomly now getting the error Property 'toMatchSnapshot' does not exist on type 'Assertion'. across all my snapshot tests.

Here is an example test where I am getting this error:

import { BrowserRouter } from 'react-router-dom';
import renderer from 'react-test-renderer';


describe('<Button />', () => {
  test('Button should render with mandatory props', () => {
    const tree = renderer
      .create(
        <BrowserRouter>
          <Button text="Test Text" />
        </BrowserRouter>
      )
      .toJSON();
    expect(tree).toMatchSnapshot(); <-- Error here
  });
}

I have tried various solutions such as updating Jest, and updating related packages and nothing seems to work.

My Package.json:

{
  "name": "webapp",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "start": "react-scripts start",
    "cypress:open": "cypress open"
  },
  "dependencies": {
    "@babel/core": "^7.19.1",
    "@babel/plugin-syntax-flow": "^7.18.6",
    "@babel/plugin-transform-react-jsx": "^7.19.0",
    "@react-spring/web": "^9.5.5",
    "@types/socket.io-client": "^3.0.0",
    "axios": "^0.27.2",
    "formik": "^2.2.9",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-error-boundary": "^3.1.4",
    "react-router-dom": "^6.4.3",
    "react-scripts": "^5.0.1",
    "socket.io-client": "^4.5.1",
    "uuid": "^9.0.0",
    "yup": "^0.32.11"
  },
  "devDependencies": {
    "@badeball/cypress-cucumber-preprocessor": "^13.0.3",
    "@bahmutov/cypress-esbuild-preprocessor": "^2.1.5",
    "@types/cypress-cucumber-preprocessor": "^4.0.1",
    "@types/jest": "^29.0.3",
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "@types/react-test-renderer": "^18.0.0",
    "autoprefixer": "^10.4.7",
    "cypress": "^10.11.0",
    "eslint": "^8.20.0",
    "jest": "^29.0.3",
    "jest-environment-jsdom": "^29.1.2",
    "postcss": "^8.4.14",
    "react-test-renderer": "^18.2.0",
    "tailwindcss": "^3.1.6",
    "ts-jest": "^29.0.1",
    "typescript": "^4.8.3"
  },
  "jest": {
    "clearMocks": true,
    "transform": {
      "^.+\\.(ts|tsx|js|jsx)$": "ts-jest"
    },
    "testEnvironment": "jsdom"
  },
  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true,
    "stepDefinitions": "cypress/e2e/**/*.ts"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I am using CRA to build a web app that also uses Typescript, and Jest for (snapshot) testing. I am randomly now getting the error Property 'toMatchSnapshot' does not exist on type 'Assertion'. across all my snapshot tests.

Here is an example test where I am getting this error:

import { BrowserRouter } from 'react-router-dom';
import renderer from 'react-test-renderer';


describe('<Button />', () => {
  test('Button should render with mandatory props', () => {
    const tree = renderer
      .create(
        <BrowserRouter>
          <Button text="Test Text" />
        </BrowserRouter>
      )
      .toJSON();
    expect(tree).toMatchSnapshot(); <-- Error here
  });
}

I have tried various solutions such as updating Jest, and updating related packages and nothing seems to work.

My Package.json:

{
  "name": "webapp",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "start": "react-scripts start",
    "cypress:open": "cypress open"
  },
  "dependencies": {
    "@babel/core": "^7.19.1",
    "@babel/plugin-syntax-flow": "^7.18.6",
    "@babel/plugin-transform-react-jsx": "^7.19.0",
    "@react-spring/web": "^9.5.5",
    "@types/socket.io-client": "^3.0.0",
    "axios": "^0.27.2",
    "formik": "^2.2.9",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-error-boundary": "^3.1.4",
    "react-router-dom": "^6.4.3",
    "react-scripts": "^5.0.1",
    "socket.io-client": "^4.5.1",
    "uuid": "^9.0.0",
    "yup": "^0.32.11"
  },
  "devDependencies": {
    "@badeball/cypress-cucumber-preprocessor": "^13.0.3",
    "@bahmutov/cypress-esbuild-preprocessor": "^2.1.5",
    "@types/cypress-cucumber-preprocessor": "^4.0.1",
    "@types/jest": "^29.0.3",
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "@types/react-test-renderer": "^18.0.0",
    "autoprefixer": "^10.4.7",
    "cypress": "^10.11.0",
    "eslint": "^8.20.0",
    "jest": "^29.0.3",
    "jest-environment-jsdom": "^29.1.2",
    "postcss": "^8.4.14",
    "react-test-renderer": "^18.2.0",
    "tailwindcss": "^3.1.6",
    "ts-jest": "^29.0.1",
    "typescript": "^4.8.3"
  },
  "jest": {
    "clearMocks": true,
    "transform": {
      "^.+\\.(ts|tsx|js|jsx)$": "ts-jest"
    },
    "testEnvironment": "jsdom"
  },
  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true,
    "stepDefinitions": "cypress/e2e/**/*.ts"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
Share Improve this question edited Jun 15, 2023 at 8:32 Benji asked Jan 4, 2023 at 21:52 BenjiBenji 591 silver badge11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It looks like you have cypress installed, which seems to be conflicting with Jest's types. See Cypress causing type errors in jest assertions

Post a comment

comment list (0)

  1. No comments so far