【问题标题】:Cypress - first test randomly fails with "Invalid or unexpected token"赛普拉斯 - 第一次测试随机失败,出现“无效或意外令牌”
【发布时间】:2021-09-29 01:33:25
【问题描述】:

最近切换到在我们的管道中为我们的 Angular 项目使用 Cypress 并行。我们在 AWS 上的 Codebuild 上运行,并运行 Cypress runner 的 5 个线程。大约四分之一的时间,对其中一个线程的第一次测试失败并出现以下错误:

An uncaught error was detected outside of a test
Invalid or unexpected token
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test. We dynamically generated a new test to display this failure.

尝试了很多方法来解决这个问题,包括将 modifyObtrusiveCode 设置为 false、chromeWebSecurity 设置为 false、升级 Cypress。我们已经在捕获未捕获的异常,因此这似乎不应该成为问题。我为此打开了一些额外的日志,这是输出

[3] 2020-03-06T19:57:20.369Z cypress:server:project onMocha start
[3] 2020-03-06T19:57:20.369Z cypress:server:reporter got mocha event 'start' with args: [ { start: '2020-03-06T19:57:20.366Z' } ]
[3] 2020-03-06T19:57:20.374Z cypress:server:project onMocha suite
[3] 2020-03-06T19:57:20.374Z cypress:server:reporter got mocha event 'suite' with args: [ { id: 'r1', title: '', root: true, type: 'suite', file: 'cypress/integration/ci-tests/content-acquisition/channels/channel-manual-upload-run-acquired-items-tab.spec.ts' } ]
[3]
[3] 2020-03-06T19:57:20.390Z cypress:server:project onMocha test
[3] 2020-03-06T19:57:20.391Z cypress:server:reporter got mocha event 'test' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', body: 'function throwErr() {\n throw err;\n }', type: 'test' } ]

[3] 2020-03-06T19:57:20.555Z cypress:server:reporter got mocha event 'fail' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', err: { message: 'Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.', name: 'Uncaught SyntaxError', stack: 'Uncaught SyntaxError: Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.' }, state: 'failed', body: 'function throwErr() {\n      throw err;\n    }', type: 'test', duration: 179, wallClockStartedAt: '2020-03-06T19:57:20.374Z', timings: { lifecycle: 26, test: [Object] } } ]

我真的不能犯这些错误,但也许其他人可以。我有点不知道该尝试什么(我今天尝试的东西比我列出的要多,但我想不起来了)。有什么想法吗?

【问题讨论】:

  • 我也遇到了这个问题,但一直没能找到解决办法。分享我所做的,希望它可能会激发一些想法。我让每个线程都设置了自己的 appdata 位置。我正在使用 webpack 预处理器,并认为可能某些输出文件在某种竞争条件下相互覆盖。

标签: cypress


【解决方案1】:

modifyObtrusiveCode 设置为 false 并不能帮助您作为 https://github.com/cypress-io/cypress/issues/6132 中的人 .. 当我在 Cypress 中遇到类似的 flakey “unexpected ..” 错误时,我可以给出我的调试过程:

cypress run 有一个 burn= 参数,可以反复运行。使用 cypress-har-generator 插件为那些运行启用 .har 输出记录。

当您有两组成功和失败的示例 .har 文件用于同一请求时,请在浏览器中打开它们以比较是否有任何突出的地方。

我在 .har 文件上使用了 diff + jq 查询来比较每个请求路径的组内容,但是已经在浏览器检查器网络选项卡中打开失败的 .har 显示 .js 路径的处理时间为 30 秒,而该路径最终是不完整,因此违反了js语法,导致unexpected end of input错误,类似于你的“意外令牌”。

有趣的是,这发生在同一代码行的同一文件中,暗示赛普拉斯存在解析问题。

我们交换了那个依赖项(或者特别是更新了它并改变了它的 webpacked 方式),赛普拉斯停止了对资源的打嗝,片状消失了。

我的印象是,运行赛普拉斯的并行线程会导致问题发生。

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多