lmiller1990 / cypress-react-template

Component Testing with Cypress and React
15 stars 12 forks source link

ReferenceError: Cypress is not defined #3

Closed mosheKerbel closed 3 years ago

mosheKerbel commented 3 years ago

Following the blog post tutoruial or clonning the repository results with the same error:

 Pattern matches 1 file
 › src/App.spec.tsx
 FAIL  src/App.spec.tsx
  ● Test suite failed to run

    ReferenceError: Cypress is not defined

      1 | import React from 'react';
    > 2 | import { mount } from '@cypress/react';
        | ^

Steps to reproduce:

  1. Clone the reposory
  2. yarn
  3. yarn test app.spec
tonycaputome commented 3 years ago

Same problem here

import React from "react";
import { mount } from "@cypress/react";

import App from "./App";

it("renders learn react link", () => {
  mount(<App />);
  cy.get("a").contains("Learn React");
  cy.get("img").should("have.class", "App-logo");
});
ERROR in 
src/App.test.js
  Line 18:3:  'cy' is not defined  no-undef
  Line 19:3:  'cy' is not defined  no-undef
tonycaputome commented 3 years ago

I solved the issue in this way:

Add the ESlint plugin

yarn add -D eslint-plugin-cypress

Then in the package.json

"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest",
       # add this line
      "plugin:cypress/recommended" 
    ]
  },
lmiller1990 commented 3 years ago

Sorry guys, I just saw this - been swamped with issues in the main Cypress repo. Going over this template now and making sure everything is up to date.

lmiller1990 commented 3 years ago

I cannot reproduce this. I deleted my repo, recloned it, did yarn install and yarn cypress open-ct. Don't run yarn test app.spec. That will run Jest, which also comes with Create React App, trying to execute Cypress outside of Cypress (so no global cy is available. Can you please try running yarn cypress open-ct? Make sure you clone the latest version to have the latest dependencies - we did a patch since the initial release.

When running yarn cypress open-ct, everything seems okay. What command are you guys running? Can someone maybe create a bash script with the exact things they ran to reproduce the problem?

Here's what I see:

image

tonycaputome commented 3 years ago

I think, in my case the problem was renaming the .spec.tsx to .spec.js. If I keep the .tsx everything works.

lmiller1990 commented 3 years ago

So if I rename from tsx to jsx I can reproduce? I'll give that a go - all extensions should be supported.

tonycaputome commented 3 years ago

I tried only the .spec files.