尽管 cy.on('uncaught:exception') 赛普拉斯未捕获断言错误

Att*_*s29 13 cypress

关于以下错误:

Uncaught Error: Script error.
Cypress detected that an uncaught error was thrown from a cross origin script.
We cannot provide you the stack trace, line number, or file where this error occurred.
Run Code Online (Sandbox Code Playgroud)

参考https://docs.cypress.io/api/events/catalog-of-events.html#To-catch-a-single-uncaught-exception

我正在尝试运行填写表单并单击按钮提交的测试:

it('adds biological sample with maximal input', function(){
    cy.on('uncaught:exception', (err, runnable) => {
    expect(err.message).to.include('of undefined')
      done()
      return false
    });
    cy.get('a').contains('Add biological sample').click();

 . . . 

    cy.contains('Action results');
  });
Run Code Online (Sandbox Code Playgroud)

尽管我的规范包含以下内容,但我还是收到了错误消息:

    cy.on('uncaught:exception', (err, runnable) => {
    expect(err.message).to.include('of undefined')
      done()
      return false
    });
Run Code Online (Sandbox Code Playgroud)

这是测试失败的图像 测试失败.

左下角的错误是,

错误:未捕获的断言错误:预期的 '$f 未定义\n\n此错误源自您的应用程序代码,而不是来自赛普拉斯。\n\n当 Cypress 检测到源自您的应用程序的未捕获错误时,它将自动使当前测试失败。\n\n此行为是可配置的,您可以选择通过侦听 \'uncaught:exception\' 事件来关闭它。\ n\nhttps://on.cypress.io/uncaught-exception-from-application' 包含'of undefined' ( https://www.flukebook.org/_cypress/runner/cypress_runner.js:49186 )

似乎我正在接受赛普拉斯的建议并没有得到想要的结果。有什么建议?这有发生在其他人身上吗?

soc*_*way 13

您能否从 cypress 异常块中删除expect(err.message).to.include('of undefined')和 并done()在测试中添加以下代码并再次运行测试

Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
    return false
})
Run Code Online (Sandbox Code Playgroud)


eme*_*ery 7

The easiest way to fix this is to add the following to the top of your spec:

Cypress.on('uncaught:exception', (err, runnable) => {
  return false;
});
Run Code Online (Sandbox Code Playgroud)

This gets the same indentation level as your "it" blocks, nested directly under "describe". It will cause cypress to ignore all uncaught JS exceptions.

In the question, Atticus29 expects "of undefined" to be present in the error message, but the error doesn't actually contain that string. He could change

expect(err.message).to.include('of undefined')
Run Code Online (Sandbox Code Playgroud)

to

expect(err.message).to.include('is not defined')
Run Code Online (Sandbox Code Playgroud)

then it will pass.

关闭规范中所有未捕获的异常处理(推荐) https://docs.cypress.io/api/events/catalog-of-events.html#To-turn-off-all-uncaught-exception-handling

捕获单个未捕获的异常并断言它包含一个字符串 https://docs.cypress.io/api/events/catalog-of-events.html#To-catch-a-single-uncaught-exception