首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jest spyOn仅在第二次调用和第三次调用时模拟实现

Jest spyOn仅在第二次调用和第三次调用时模拟实现
EN

Stack Overflow用户
提问于 2020-04-27 09:40:26
回答 1查看 4.9K关注 0票数 6

我有一个函数,我只想在第二次调用和第三次调用时模拟它,但在第一次调用时使用默认实现。我看过Jest文档,有一个函数mockImplementationOnce可以用来模拟单个调用的实现。

有没有一个我可以使用的函数,让它对第一次调用使用默认实现,而只模拟第二次和第三次调用?

代码语言:javascript
运行
复制
let functionCalls = 0;
const database = {
  fetchValues() {
    fetchValues();
    fetchValues();
  },
};
jest.spyOn(database, 'fetchValues')
.useDefaultImplementation() // <-- is there such a function?
.mockImplementationOnce(() => 42)
.mockImplementationOnce(() => 42)
EN

回答 1

Stack Overflow用户

发布于 2020-04-27 11:11:21

您可以使用mockImplementation方法模拟默认实现。请参阅mock-implementations

例如。

代码语言:javascript
运行
复制
const database = {
  fetchValues() {
    return 'real data';
  },
};

describe('61450440', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });
  it('should pass', () => {
    jest
      .spyOn(database, 'fetchValues')
      .mockImplementation(() => 'default')
      .mockImplementationOnce(() => 'first call')
      .mockImplementationOnce(() => 'second call');

    console.log(
      [database.fetchValues(), database.fetchValues(), database.fetchValues(), database.fetchValues()].join(','),
    );
  });
  it('should pass too', () => {
    jest
      .spyOn(database, 'fetchValues')
      .mockImplementation(() => 'real data')
      .mockImplementationOnce(() => 'real data')
      .mockImplementationOnce(() => 'first call')
      .mockImplementationOnce(() => 'second call');

    console.log(
      [database.fetchValues(), database.fetchValues(), database.fetchValues(), database.fetchValues()].join(','),
    );
  });

  it('should pass 3', () => {
    const fetchValuesSpy = jest.spyOn(database, 'fetchValues');
    console.log('call original fetchValues:', database.fetchValues());
    fetchValuesSpy.mockImplementationOnce(() => 'first call').mockImplementationOnce(() => 'second call');
    console.log('call mocked fetchValues:', database.fetchValues(), database.fetchValues());
    console.log('call original fetchValues again:', database.fetchValues());
  });
});

测试结果:

代码语言:javascript
运行
复制
 PASS  stackoverflow/61450440/index.test.ts (13.748s)
  61450440
    ✓ should pass (20ms)
    ✓ should pass too (1ms)
    ✓ should pass 3 (12ms)

  console.log stackoverflow/61450440/index.test.ts:15
    first call,second call,default,default

  console.log stackoverflow/61450440/index.test.ts:27
    real data,first call,second call,real data

  console.log stackoverflow/61450440/index.test.ts:34
    call original fetchValues: real data

  console.log stackoverflow/61450440/index.test.ts:36
    call mocked fetchValues: first call second call

  console.log stackoverflow/61450440/index.test.ts:37
    call original fetchValues again: real data

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        15.761s
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61450440

复制
相关文章

相似问题

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