Home > Enterprise >  ReferenceError: context is not defined when using cypress with testing-library.com
ReferenceError: context is not defined when using cypress with testing-library.com

Time:04-16

Here's my test in ./tests/homepage.test.js

context('Signup flow', () => {
    it('The happy path should work', () => {
        cy.visit('https://grazily.com/register');
        const random = Math.floor(Math.random() * 100000);
        cy.get('[data-testid=username]').type(`Tester${random}`);
        cy.get('[data-testid=email]').type(`user ${random}@realworld.io`);
        cy.get('[data-testid=password]').type('mysupersecretpassword');
        cy.get('[data-testid=signup-button]').click();
        cy.get('[data-testid=no-articles-here]', { timeout: 10000 }).should('be.visible');

Here's the file ./cypress/support/commands.js

import '@testing-library/cypress/add-commands';

tsconfig.json:

"compilerOptions": {
        "types": ["cypress", "@testing-library/cypress"],
...
}

I didn't make any other changes...and get this error when running npm t

I think t uses jest ./src so I'm not sure how to invoke cypress in ./tests/*test.js folder

Update:

Your pluginsFile is invalid: /home/ettinger/src/oblivion/Catalyze-frontend/cypress/plugins/index.js

Getting this error now with cypress run test

CodePudding user response:

You're correct, npm t runs a predefined command specified in the "test" property of a package's "scripts" object.

//package.json
{
  ...
  "scripts": {
    "test": "jest"
  }
}

so you can either change "jest" to "cypress run" or add extra scripts, for example

//package.json
{
  ...
  "scripts": {
    "test": "jest",
    "e2e": "cypress run",
    "cy:open": "cypress open",
  }
}

or just run "npx cypress open" instead of "npm t".

BTW context(...) is valid in a Cypress test.

CodePudding user response:

Getting this error now with cypress run test

If you are trying to run a specific test, add --spec

npm run cypress run --spec my-test.spec.js
  • Related