cypress-io / cypress-grep

Filter tests using substring
137 stars 19 forks source link

Cypress@10.3.1 - ReferenceError: it is not defined #139

Closed MartinPilny closed 2 years ago

MartinPilny commented 2 years ago

Hello, I try to upgrade from Cypress@9.6.1 to Cypress@10.3.1, but I am getting following error

drawing

Could anybody help please?

Stack trace

ReferenceError: it is not defined
    at Object. (C:\Users\user\Documents\work\project\node_modules\cypress-grep\src\support.js:10:13)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Object.require.extensions. [as .js] (C:\Users\user\AppData\Local\Cypress\Cache\10.3.1\Cypress\resources\app\node_modules\ts-node\src\index.ts:1445:43)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object. (C:\Users\user\Documents\work\project\cypress\support\e2e.ts:21:1)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Module.m._compile (C:\Users\user\AppData\Local\Cypress\Cache\10.3.1\Cypress\resources\app\node_modules\ts-node\src\index.ts:1455:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Object.require.extensions. [as .ts] (C:\Users\user\AppData\Local\Cypress\Cache\10.3.1\Cypress\resources\app\node_modules\ts-node\src\index.ts:1458:12)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)

package.json

{
  "name": "project",
  "version": "2.0.0",
  "main": "index.js",
  "private": true,
  "devDependencies": {
    "@types/chai": "^4.3.0",
    "@types/mocha": "^9.1.0",
    "@types/node": "^16.11.10",
    "@typescript-eslint/eslint-plugin": "^5.9.1",
    "@typescript-eslint/parser": "^5.9.1",
    "azure-devops-node-api": "^11.1.1",
    "chai": "^4.3.6",
    "cypress": "10.3.1",
    "cypress-file-upload": "^5.0.8",
    "cypress-fill-command": "^1.0.2",
    "cypress-grep": "^3.0.2",
    "cypress-mailosaur": "^2.11.0",
    "cypress-real-events": "^1.7.1",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-chai-friendly": "^0.7.2",
    "eslint-plugin-cypress": "^2.12.1",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.2.0",
    "mocha": "^9.2.1",
    "moment": "^2.29.1",
    "ts-node": "^10.7.0",
    "typescript": "^4.5.2"
  },
  "dependencies": {
    "axios": "^0.26.0",
    "har-validator": "^5.1.5",
    "lambdatest-cypress-cli": "^2.1.8",
    "ramda": "^0.27.1"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "preserveConstEnums": true,
    "noImplicitAny": true,
    "declaration": true,
    "declarationMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "target": "es5",
    "module": "commonjs",
    "allowJs": true,
    "outDir": "./dist/",
    "declarationDir": "./dist/",
    "skipLibCheck": true,
    "moduleResolution": "node",
    "types": ["cypress", "node", "cypress-file-upload", "cypress-real-events", "cypress-grep", "cypress-mailosaur"],
    "lib": ["es2015", "dom", "ES5"]
  },
  "include": ["**/*.ts", "src", "reporters"],
  "exclude": ["node_modules"]
}

cypress/support/e2e.ts

...
import registerCypressGrep from 'cypress-grep'
registerCypressGrep()
...

cypress.config.ts

import { defineConfig } from 'cypress'

export default defineConfig({
  screenshotsFolder: 'cypress_screenshots',
  screenshotOnRunFailure: true,
  defaultCommandTimeout: 35000,
  pageLoadTimeout: 120000,
  viewportWidth: 375,
  viewportHeight: 667,
  chromeWebSecurity: false,
  projectId: 'xxxx',
  retries: 1,
  video: false,
  e2e: {
    // We've imported your old cypress plugins here.
    // You may want to clean this up later by importing these.
    setupNodeEvents (on, config) {
      require('cypress-grep/src/plugin')(config)
      require('./cypress/plugins/index.js')(on, config)
      return config
    },
    specPattern: 'specs/**/*.{js,jsx,ts,tsx}',
    baseUrl: 'https://www.example.com/'
  }
})

cypress/plugins/index.js

/// <reference types="cypress" />

// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
  require('cypress-grep/src/plugin')(config)

  on('task', {
    log (message) {
      console.log(message)
      return null
    }
  })

  return config
}
MartinPilny commented 2 years ago

This issue doesn't bother me anymore. I'm not exactly sure how I solved that but I was going by approach to create new Cypress project and then adding code from current project per partes.