开玩笑toMatchSnapshot没有引发异常 [英] Jest toMatchSnapshot not throwing an exception

查看:106
本文介绍了开玩笑toMatchSnapshot没有引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数笑话的期望(arg1).xxxx如果比较未达到期望,则()方法将引发异常.此模式的一个例外似乎是toMatchSnapshot()方法.似乎永远不会抛出异常,而是存储故障信息以供以后的Jest代码处理.

Most of Jest's expect(arg1).xxxx() methods will throw an exception if the comparison fails to match expectations. One exception to this pattern seems to be the toMatchSnapshot() method. It seems to never throw an exception and instead stores the failure information for later Jest code to process.

如何使toMatchSnapshot()引发异常?如果这不可能,我们的测试还有另一种方法可以检测快照比较何时失败?

How can we cause toMatchSnapshot() to throw an exception? If that's not possible, is there another way that our tests can detect when the snapshot comparison failed?

推荐答案

这将起作用!运行toMatchSnapshot断言后,检查全局状态:expect(global[GLOBAL_STATE].state.snapshotState.matched).toEqual(1);

This will work! After running your toMatchSnapshot assertion, check the global state: expect(global[GLOBAL_STATE].state.snapshotState.matched).toEqual(1);

仅花了最后一个小时就试图为我们自己的测试弄清楚.尽管Jest的维护者也许可以告诉我访问Symbol.for('$$jest-matchers-object')是否是一个好主意,但是这对我来说也并不客气.这是上下文的完整代码段:

Just spent the last hour trying to figure it out for our own tests. This doesn't feel hacky to me either, though a maintainer of Jest may be able to tell me whether accessing Symbol.for('$$jest-matchers-object') is a good idea or not. Here's a full code snippet for context:

const GLOBAL_STATE = Symbol.for('$$jest-matchers-object');

describe('Describe test', () => {
  it('should test something', () => {
    try {
      expect({}).toMatchSnapshot(); // replace with whatever you're trying to test
      expect(global[GLOBAL_STATE].state.snapshotState.matched).toEqual(1);
    } catch (e) {
      console.log(`\x1b[31mWARNING!!! Catch snapshot failure here and print some message about it...`);
      throw e;
    }
  });
});

这篇关于开玩笑toMatchSnapshot没有引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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