1. 在toEqual或toBeCalledWith2. 匹配arraycontains中的元素3. 匹配objectContaining 或者toMatchObject的属性 这个示例还展示了如何使用expect嵌套多个不对称的匹配器。在expect.arrayContaining stringMatching。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 describe('stringMatching in arrayContaining', () =...
你可以在内部使用toEqual或toBeCalledWith而不是文字值。例如如果你想检查一个模拟函数是否被调用,它的参数是非空的: test('map calls its argument with a non-null argument', () => { const mock = jest.fn(); [1].map(x => mock(x)); expect(mock).toBeCalledWith(expect.anything()); }); ...
toBeCalledWith( expect.objectContaining({ x: expect.any(Number), y: expect.any(Number), }), ); }); expect.stringMatching(string | regexp) 匹配与预期regexp匹配的接收字符串,你可以用它代替文字的值: 在toEqual或toBeCalledWith 匹配arraycontains中的元素 匹配objectContaining 或者toMatchObject的属性 ...
toThrow - 要测试的特定函数会在调用时抛出一个错误 .resolves 和 .rejects - 用来测试 promise .toHaveBeenCalled() - 用来判断一个函数是否被调用过 .toHaveBeenCalledTimes(number) - 判断函数被调用过几次 toThrow 要测试的特定函数会在调用时抛出一个错误 ...
expect(myMockFn).toHaveBeenCalled(); ``` 上面的代码中,我们创建了一个名为`myMockFn`的Mock函数,并调用它。然后使用Jest提供的`toHaveBeenCalled()`方法来验证该函数是否被调用过。 除了`toHaveBeenCalled()`方法,Jest还提供了很多其他的验证方法,如`toHaveBeenCalledTimes()`、`toHaveBeenCalledWith()`...
was the stub/spy called? was the stub/spy called the right amount of times? was the stub/spy called with the right arguments/parameters? jest.toBeCalled()/.toHaveBeenCalled(): assert a stub/spy has been called In Jest, stubs are instantiated withjest.fn()and they’re used withexpect(...
这样太棒了,我们确实模拟了axios,所以真实的axios就不会被调用来发起HTTP请求。而且,我们甚至用toBeCalledWith验证了axios会被传入参数并调用。但是我们仍然遗漏了某些事情:我们没有检查返回的promise。 First we need to make our mocked axios.get method to return a promise. jest.fn accepts a factory function...
expect(setValue).toHaveBeenCalledWith(-1); }); }); 而使用 React Testing Library 编写的单元测试还是可以正常运行的,因为它更加关注应用的事件处理,以及展示;而非应用的实现细节,以及状态变化。更加符合我们对于单元测试的原本诉求,以及最佳实践。
√ Choose the test environment that will be used for testing » jsdom (browser-like) √ Do you want Jest to add coverage reports? ... yes √ Which provider should be used to instrument code for coverage? » babel √ Automatically clear mock calls, instances and results before every te...
('toto'); return expect(MySuperFunc.superFunc).toHaveBeenCalledWith('toto');}); 但是,在示例2中,如果您通过以下方式更改导出: // MySuperFunc.tsxexport const MyVariable = {superFunc: () => 'hello world'}); 您的jest.mock必须修改为: jest.mock('./MySuperFunc', () => ({MyVariable:...