首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用toHaveBeenNthCalledWith时响应Jest测试错误

使用toHaveBeenNthCalledWith时响应Jest测试错误
EN

Stack Overflow用户
提问于 2019-02-05 17:30:48
回答 1查看 7.1K关注 0票数 4

我正在跟踪Jest的文档,但是我无法避免下面的错误。expect(dummyFunction).toHaveBeenNthCalledWith is not a function

除非我遗漏了什么,否则我非常肯定,我的dummyFunction设置作为jest.fn()是正确的。在测试中使用dummyFunction之前,我甚至安慰了它的输出,这就是输出。

dummyFunction console.log输出

代码语言:javascript
运行
复制
    { [Function: mockConstructor]
      _isMockFunction: true,
      getMockImplementation: [Function],
      mock: [Getter/Setter],
      mockClear: [Function],
      mockReset: [Function],
      mockReturnValueOnce: [Function],
      mockReturnValue: [Function],
      mockImplementationOnce: [Function],
      mockImplementation: [Function],
      mockReturnThis: [Function],
      mockRestore: [Function] }

toHaveBeenCalledNthWith测试

代码语言:javascript
运行
复制
const dummyFunction = jest.fn();

expect(dummyFunction).toHaveBeenCalledTimes(2); // pass

expect(dummyFunction).toHaveBeenNthCalledWith(1, { foo: 'bar' }); // error
expect(dummyFunction).toHaveBeenNthCalledWith(2, { please: 'work' });

提前谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-06 07:25:17

toHaveBeenNthCalledWith是在Jest版本23.0.0中发布的,所以如果您使用的是较早版本的Jest,您将看到该错误。

请注意,toHaveBeenNthCalledWith只是spy.mock.calls[nth],所以如果您使用的是较早版本的Jest,则只需执行以下操作:

代码语言:javascript
运行
复制
const dummyFunction = jest.fn();

dummyFunction({ foo: 'bar' });
dummyFunction({ please: 'work' });

expect(dummyFunction).toHaveBeenCalledTimes(2); // pass

expect(dummyFunction.mock.calls[0]).toEqual([{ foo: 'bar' }]); // pass
expect(dummyFunction.mock.calls[1]).toEqual([{ please: 'work' }]); // pass
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54540006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档