带有放大js的Cypress-ReferenceError:global未定义



我刚刚升级到Cypress 10,现在收到来自放大身份验证库的问题,我正在使用该库将交互式用户登录到测试中的网站。我为此做了一个cy扩展。

sdk中的一个已知问题是,它使用了global变量,该变量在角度上通过多填充来克服

/**
*  AWS Amplify - Currently, the newest versions of Angular (6+) do not provide the shim for the
*   global object which was provided in previous versions.
*/
(window as any).global = window;

我已经尝试在Cypress 10:的许多地方添加了它

扩展文件中的
  • 在柏树配置文件中
  • 在支持文件中

但运气不好。

FWIW,扩展的要点

这是完整的堆栈跟踪:

ReferenceError以下错误源于您的测试代码,而不是来自赛普拉斯。

global未定义

Cypress检测到源自测试代码的未捕获错误时它将自动使当前测试失败。

Cypress无法将此错误与任何特定测试关联起来。

我们动态生成了一个新的测试来显示此失败。看法堆栈跟踪打印到控制台在node_modules/aamazon cognito identity js/node_modules/buffer/index.js(http://localhost:4200/__cypress/tests?p=cypress\支持\ts:12878:37(在__require2(http://localhost:4200/__cypress/tests?p=cypress\support\e2.ts:17:52(在eval(http://localhost:4200/__cypress/tests?p=cypress\支持\ts:27843:31(在eval(http://localhost:4200/__cypress/tests?p=cypress\support\e2.ts:33508:3(at eval((来自上一个事件:在runScriptsFromUrls(http://localhost:4200/__cypress/runner/cypress_runner.js:165206:136)在Object.runScripts(http://localhost:4200/__cypress/runner/cypress_runner.js:165221:12)在$Cypress.onSpecWindow(http://localhost:4200/__cypress/runner/cypress_runner.js:153378:75)

我试着在扩展文件的顶部添加这个:

let global = {};
(window as any).global = window;
/**
* amplify-js / cognito auth helper
* specific personas are logged-in and their tokens are cached to save on round-trips.
*/
import Auth, { CognitoUser } from '@aws-amplify/auth';
import Amplify from '@aws-amplify/core';

问题在于新的esbuild捆绑器,它显然是WIP,但某些灵魂已经填充了polyfill。

(这一切意味着什么我不知道(

我之前用新的npmoverrides指令解决了这个问题

"overrides": {
"@aws-amplify/auth": {
"amazon-cognito-identity-js": {
"buffer": "6.0.3"
}
}
}

我不喜欢它,因为它现在和将来都有潜在的副作用。

Polyfilling节点是更好的方法,这里将更详细地讨论,但简而言之,更新Cypress配置:

export default defineConfig({
e2e: {
async setupNodeEvents(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions): Promise<Cypress.PluginConfigOptions> {
// This is required for the preprocessor to be able to generate JSON reports after each run, and more,
await addCucumberPreprocessorPlugin(on, config);
const bundler = createBundler({
plugins: [
NodeModulesPolyfills(),
GlobalsPolyfills({
process: true,
buffer: true
}),
createEsbuildPlugin(config)
]
});
on('file:preprocessor', bundler);

我在运行柏树测试时遇到了两个问题,我需要下载csv或excel文件,再读取这些文件并验证头和行数-我在https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__download但在运行基于路径和整洁csv导入的测试时遇到问题,即未定义进程或未定义缓冲区。我能够在上述解决方案的基础上解决上述问题,但略有变化。。

npm install@ebuild-plugins/node-globals polyfill

npm install@ebuild插件/节点模块polyfill

在cypress.config.js 下

const{NodeGlobalsPolyfillPlugin}=require("@esbuild-plugins/node-globalspolyfill"(

const{NodeModulesPolyfillPlugin}=require("@sbuild-plugins/node-modulespolyfill"(

e2e段将

experimentalSessionAndOrigin: true,
async setupNodeEvents(on, config ){
const bundler = createBundler({
plugins: [
NodeModulesPolyfillPlugin(),
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true
}),
createEsbuildPlugin(config)
],
});
on("file:preprocessor", bundler);
await addCucumberPreprocessorPlugin(on, config);
return config;
},
specPattern: "cypress/e2e/features/**/*.feature",
chromeWebSecurity: false,
}```

最新更新