开箱即用的 Jasmine 提供的匹配器。
成员
not :matchers
反转跟随此 期望 的匹配器
类型
- 自
- 1.3.0
示例
expect(something).not.toBe(true);
方法
nothing()
期望 没有任何内容。
- 自
- 2.8.0
示例
expect().nothing();
toBe(expected)
期望 实际值===为期望值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 要比较的预期值。 |
- 自
- 1.3.0
示例
expect(thing).toBe(realThing);
toBeCloseTo(expected, precisionopt)
期望 实际值在期望值的指定精确度之内。
参数
| 名称 | 类型 | 属性 | 默认 | 说明 |
|---|---|---|---|---|
expected |
对象 | 要比较的预期值。 |
||
precision |
数字 | <可选> |
2 | 要检查的小数位数。 |
- 自
- 1.3.0
示例
expect(number).toBeCloseTo(42.2, 3);
toBeDefined()
期望 实际值被定义。(非 undefined)
- 自
- 1.3.0
示例
expect(result).toBeDefined();
toBeFalse()
期望 实际值为 false。
- 自
- 3.5.0
示例
expect(result).toBeFalse();
toBeFalsy()
期望 实际值为假
- 自
- 2.0.0
示例
expect(result).toBeFalsy();
toBeGreaterThan(expected)
期望 实际值大于期望值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
数字 | 比较目标的值。 |
- 自
- 2.0.0
示例
expect(result).toBeGreaterThan(3);
toBeGreaterThanOrEqual(expected)
expect 预期的值大于或等于实际值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
数字 | 要比较的预期值。 |
- 自
- 2.0.0
示例
expect(result).toBeGreaterThanOrEqual(25);
toBeInstanceOf(expected)
expect 实际值是预期的类的实例
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 要检查的类或构造函数 |
- 自
- 3.5.0
示例
expect('foo').toBeInstanceOf(String);
expect(3).toBeInstanceOf(Number);
expect(new Error()).toBeInstanceOf(Error);
toBeLessThan(expected)
expect 预期的值小于实际值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
数字 | 要比较的预期值。 |
- 自
- 2.0.0
示例
expect(result).toBeLessThan(0);
toBeLessThanOrEqual(expected)
expect 预期的值小于或等于实际值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
数字 | 要比较的预期值。 |
- 自
- 2.0.0
示例
expect(result).toBeLessThanOrEqual(123);
toBeNaN()
expect 实际值为 NaN(非数字)。
- 自
- 1.3.0
示例
expect(thing).toBeNaN();
toBeNegativeInfinity()
expect 实际值等于 -Infinity(负无穷)。
- 自
- 2.6.0
示例
expect(thing).toBeNegativeInfinity();
toBeNull()
expect 实际值为 null。
- 自
- 1.3.0
示例
expect(result).toBeNull();
toBePositiveInfinity()
expect 实际值为 Infinity(正无穷)。
- 自
- 2.6.0
示例
expect(thing).toBePositiveInfinity();
toBeTrue()
expect 实际值为 true。
- 自
- 3.5.0
示例
expect(result).toBeTrue();
toBeTruthy()
expect 实际值为真。
- 自
- 2.0.0
示例
expect(thing).toBeTruthy();
toBeUndefined()
expect 实际值为 undefined。
- 自
- 1.3.0
示例
expect(result).toBeUndefined():
toContain(expected)
expect 实际值包含指定的值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 要查找的值。 |
- 自
- 2.0.0
示例
expect(array).toContain(anElement);
expect(string).toContain(substring);
toEqual(expected)
expect 使用深度相等比较,实际值等于预期值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 预期值 |
- 自
- 1.3.0
示例
expect(bigObject).toEqual({"foo": ['bar', 'baz']});
toHaveBeenCalled()
- 自
- 1.3.0
示例
expect(mySpy).toHaveBeenCalled();
expect(mySpy).not.toHaveBeenCalled();
toHaveBeenCalledBefore(expected)
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
Spy |
- 自
- 2.6.0
示例
expect(mySpy).toHaveBeenCalledBefore(otherSpy);
toHaveBeenCalledOnceWith()
参数
| 类型 | 属性 | 说明 |
|---|---|---|
| 对象 | <可重复> |
需要寻找的参数 |
- 自
- 3.6.0
示例
expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar', 2);
toHaveBeenCalledTimes(expected)
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
数字 | 需要寻找的调用次数。 |
- 自
- 2.4.0
示例
expect(mySpy).toHaveBeenCalledTimes(3);
toHaveBeenCalledWith()
参数
| 类型 | 属性 | 说明 |
|---|---|---|
| 对象 | <可重复> |
需要寻找的参数 |
- 自
- 1.3.0
示例
expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);
toHaveClass(expected)
expect 实际值是具有预期类的 DOM 元素
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 要测试的类名称 |
- 自
- 3.0.0
示例
const el = document.createElement('div');
el.className = 'foo bar baz';
expect(el).toHaveClass('bar');
toHaveSize(expected)
expect 实际大小等于预期大小,使用类似数组的长度或对象键大小。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
对象 | 预期大小 |
- 自
- 3.6.0
示例
array = [1,2];
expect(array).toHaveSize(2);
toHaveSpyInteractions()
expect 实际值(SpyObj)侦听器被调用。
- 自
- 4.1.0
示例
expect(mySpyObj).toHaveSpyInteractions();
expect(mySpyObj).not.toHaveSpyInteractions();
toMatch(expected)
expect 实际值与正则表达式匹配
参数
| 名称 | 类型 | 说明 |
|---|---|---|
expected |
RegExp | String | 要查找的字符串中的值。 |
- 自
- 1.3.0
示例
expect("my string").toMatch(/string$/);
expect("other string").toMatch("her");
toThrow(expectedopt)
expect 某个函数 throw 某些内容。
参数
| 名称 | 类型 | 属性 | 说明 |
|---|---|---|---|
expected |
对象 | <可选> |
应该抛出的值。如果未提供,只需检查是否抛出某些内容。 |
- 自
- 2.0.0
示例
expect(function() { return 'things'; }).toThrow('foo');
expect(function() { return 'stuff'; }).toThrow();
toThrowError(expectedopt, messageopt)
expect 某个函数 throw 一个 Error。
参数
| 名称 | 类型 | 属性 | 说明 |
|---|---|---|---|
expected |
错误 | <可选> |
|
消息 |
RegExp | String | <可选> |
抛出的 |
- 自
- 2.0.0
示例
expect(function() { return 'things'; }).toThrowError(MyCustomError, 'message');
expect(function() { return 'things'; }).toThrowError(MyCustomError, /bar/);
expect(function() { return 'stuff'; }).toThrowError(MyCustomError);
expect(function() { return 'other'; }).toThrowError(/foo/);
expect(function() { return 'other'; }).toThrowError();
toThrowMatching(predicate)
expect 某个函数 throw 与谓词匹配的某些内容。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
谓词 |
函数 | 一个将抛出的异常作为参数并返回 true(如果匹配)的函数。 |
- 自
- 3.0.0
示例
expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });
withContext(message) → {matchers}
为 expectation 的匹配器失败添加一些上下文,以便可以更轻松地将其与类似的预期区分开来。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
消息 |
String | 匹配器失败时要显示的其他上下文 |
- 自
- 3.3.0
返回
- 类型
- matchers
示例
expect(things[0]).withContext('thing 0').toEqual('a');
expect(things[1]).withContext('thing 1').toEqual('b');