企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > [完整匹配器接口](https://jestjs.io/docs/en/expect) ## .toBe 值 匹配 ``` expect(2 + 2).toBe(4); ``` ## .toEqual `toBe`使用`Object.is`来进行精准匹配的测试。 如果您想要检查对象的值,请使用`toEqual`代替 ``` expect(data).toEqual({one: 1, two: 2}); ``` ## .toBeWithinRange ``` expect(100).toBeWithinRange(90, 110); expect(101).not.toBeWithinRange(0, 100); ``` ## .toMatch ``` expect('team').not.toMatch(/I/); expect('team').not.toMatch(/tea/); ``` ## .toContain 数组总存在某个值 ``` expect([1,2,3]).toContain(2); ``` ## .Exceptions ``` function compileAndroidCode() { throw new Error('you are using the wrong JDK'); } test('compiling android goes as expected', () => { expect(() => compileAndroidCode()).toThrow(); expect(() => compileAndroidCode()).toThrow(Error); // You can also use the exact error message or a regexp expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK'); expect(() => compileAndroidCode()).toThrow(/JDK/); }); ```