赛普拉斯-如果发生console.error,则测试失败 [英] Cypress - let test fail if console.error occurs

查看:59
本文介绍了赛普拉斯-如果发生console.error,则测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果调用console.error,我们想让Cypress中的任何测试失败.有时,控制台日志中会出现错误,这不会使测试失败.我们始终必须查看控制台窗口以检查任何错误消息

We would like to let any test in Cypress fail if console.error is called. Sometimes we have errors in the console log which does not let the test fail. We always have to look into the console window to check any error messages

推荐答案

只需将其添加到您的support/index.js

Just add this to your support/index.js

let consoleSpy;
Cypress.on('window:before:load', (win) => {
    consoleSpy = cy.spy(win.console, "error")
})
afterEach(() => {
    // consoleSpy can be null if test failed already in beforeEach 
    if (consoleSpy) {
      expect(consoleSpy).not.to.be.called
    }
})

它将在每个测试上运行,并检查是否调用了console.error,然后使测试失败.

It will run on each Test and will check if console.error was called and will then let the test fail.

这篇关于赛普拉斯-如果发生console.error,则测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆