Jest ES6 mock, 用过的多种方法,有点混乱

Jest ES6 mock, 用过的多种方法:
// 一般用这个就可以了

jest.mock('moduleName');

// 返回的是object用

jest.mock('../moduleName', () => {
  return {
    toL10n: (val) => {return val}
  }
});

// 报default之类的错用这个

jest.mock('../moduleName', () => {
  return {
    __esModule: true,
    default: (ComposedComponent) => { return ComposedComponent; }
  }
});

// 是个function用这个

jest.mock('../moduleName', () => {
  return jest.fn().mockImplementation(() => {
    return { start: jest.fn() };
  });
});

// 需要mock部分方法时用, 如下,只是mock了toL10n,其他的还用真实的。

jest.mock('moduleName', () => {
  // Require the original module to not be mocked...
  const originalModule = jest.requireActual('moduleName');
  return {
	__esModule: true, // Use it when dealing with esModules
	...originalModule,
	toL10n: jest.fn(),
  };
});
posted @ 2021-01-20 13:53  Regina_wisdom  阅读(271)  评论(0编辑  收藏  举报