匹配器
最后更新于:2022-04-02 03:13:09
[TOC]
> [完整匹配器接口](https://jestjs.io/docs/en/expect)
## .toBe
```
expect(2 + 2).toBe(4);
```
## .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/);
});
```
';