Cypress TypeError: Cannot read properties of undefined (reading 'uid')

Cypress TypeError: Cannot read properties of undefined (reading 'uid')

我有一个使用 Cucumber 预处理器支持 TypeScript 的 Cypress 项目,突然它开始抛出以下异常:

 TypeError: Cannot read properties of undefined (reading 'uid')

有时当我切换到不同的工作区时它会工作,但当我打开 Cypress runner 本身时甚至在 Jenkins 中执行时也会再次出现此错误。

赛普拉斯亚军: (v8.4.0)

Jenkins 控制台: (v8.7.0)

The function exported by the plugins file threw an error.

We invoked the function exported by `C:\Jenkins\workspace\ABC\cypress\plugins\index.js`, but it threw an error.

 TypeError: Cannot read properties of undefined (reading 'uid')
    at Object.statSync (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\node_modules\graceful-fs\polyfills.js:303:17)
    at isDirectory (C:\Jenkins\workspace\ABC\ui\node_modules\resolve\lib\sync.js:22:23)
    at loadNodeModulesSync (C:\Jenkins\workspace\ABC\ui\node_modules\resolve\lib\sync.js:191:17)
    at Function.resolveSync [as sync] (C:\Jenkins\workspace\ABC\ui\node_modules\resolve\lib\sync.js:98:17)
    at module.exports (C:\Jenkins\workspace\ABC\ui\cypress\plugins\index.js:15:25)
    at C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\lib\plugins\child\run_plugins.js:90:12
    at tryCatcher (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\node_modules\bluebird\js\release\util.js:16:23)
    at Function.Promise.attempt.Promise.try (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\node_modules\bluebird\js\release\method.js:39:29)
    at load (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\lib\plugins\child\run_plugins.js:87:7)
    at EventEmitter.<anonymous> (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\lib\plugins\child\run_plugins.js:198:5)
    at EventEmitter.emit (events.js:376:20)
    at process.<anonymous> (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\lib\plugins\util.js:19:22)
    at process.emit (events.js:376:20)
    at process.emit (C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Cypress\Cache.7.0\Cypress\resources\app\packages\server\node_modules\source-map-support\source-map-support.js:495:21)
    at emit (internal/child_process.js:910:12)
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
error Command failed with exit code 1.

plugins/index.js

const resolve = require('resolve');
const cucumber = require("cypress-cucumber-preprocessor").default;
const cypressBrowserify = require("@cypress/browserify-preprocessor");
const mysql = require('mysql2')
const db = require('../../cypress.json')
var mssql = require('mssql');
const path = require('path');
const fs = require('fs-extra');
const allureWriter = require('@shelex/cypress-allure-plugin/writer');

module.exports = (on, config) => {
  
  const options = {
    ...cypressBrowserify.defaultOptions,
    typescript: resolve.sync('typescript', {project:  config.projectRoot}),
  };  
  
  on("file:preprocessor", cucumber(options));

  on('task', {
    'createMySQLConnection'(query) {
      var value = mysqlDB(query)
      return value
    }
  });

  function mysqlDB(query) {
    var dbenv = config.env.configFile
    var connection="";
    if(dbenv=="qa"){
      connection = mysql.createConnection(db.qa)
    }
    else if(dbenv=="stg"){
      connection = mysql.createConnection(db.stg)
    }
    else if(dbenv=="stg2"){
      connection = mysql.createConnection(db.stg2)
    }
    connection.connect()
    return new Promise((resolve, reject) => {
      connection.query(query, (error, results) => {
        if (error) {
          return reject(error)
        }
        connection.end()
        return resolve(results)
  
      })
    })
  }

  on('task', {
    'createMSSQLConnection'(query) {
      return mssqlDB(query)
    }
  });


 async function mssqlDB(query){
   const connection = await mssql.connect(db.env);
   var sqlServerRequest = new mssql.Request(connection);
   return new Promise((resolve, reject) => {
    sqlServerRequest.query(query, (error, recordset) => {
      mssql.close(); 
      if(error){
      reject (error)
      }
      else{
        resolve (recordset)
      }
    }) 
  })
 };
 
  allureWriter(on, config);
  const file = config.env.configFile || 'stg2';
  const pathToConfigFile = path.resolve('cypress/config', `${file}.json`);
  return config,fs.readJson(pathToConfigFile);
  
}

support/index.ts

import './commands';
import '@shelex/cypress-allure-plugin/reporter';
import 'cypress-xpath';
import 'cypress-plugin-tab';   

Cypress.on('uncaught:exception', (err, runnable) => {
  cy.log(`Uncaught Exception: ${JSON.stringify(err)}`);

  return false;
});

我遇到了同样的问题。无论如何都是奇怪的行为,因为它发生在我尝试在我的本地机器上开始测试时。使用 git 操作工作流程开始测试时没有错误。对我来说,解决方案是来自这里的“aidanhyland”的评论: https://github.com/cypress-io/cypress/issues/19674#issuecomment-1012212527
该问题与您的代码中是否使用“uid”无关。问题在于黄瓜依赖性以及它们是如何解决的。

我已经尝试了很多事情,比如清理缓存、分析 index.js 文件,但下面的一个问题已解决。

从机器上卸载 Cypress 并安装 Cypress 8.5.0 版,一切开始正常工作

npm install cypress@8.5.0