相关疑难解决方法(0)

如何在模块取消模拟时在Jest中模拟导入的命名函数

我有以下模块我试图在Jest中测试:

// myModule.js

export function otherFn() {
  console.log('do something');
}

export function testFn() {
  otherFn();

  // do other things
}
Run Code Online (Sandbox Code Playgroud)

如上图所示,这部分出口命名的功能和重要的testFn用途otherFn.

在Jest中我正在编写单元测试时testFn,我想模拟该otherFn函数,因为我不希望错误otherFn影响我的单元测试testFn.我的问题是我不确定最好的方法:

// myModule.test.js
jest.unmock('myModule');

import { testFn, otherFn } from 'myModule';

describe('test category', () => {
  it('tests something about testFn', () => {
    // I want to mock "otherFn" here but can't reassign
    // a.k.a. can't do otherFn = jest.fn()
  });
});
Run Code Online (Sandbox Code Playgroud)

任何帮助/见解表示赞赏.

unit-testing ecmascript-6 jestjs

61
推荐指数
6
解决办法
4万
查看次数

标签 统计

ecmascript-6 ×1

jestjs ×1

unit-testing ×1