jjzjj

javascript - Jest : Mock ES6 Module with both default and named export

coder 2024-07-15 原文

我有一个带有默认导出和命名导出的 ES6 模块:

/** /src/dependency.js **/

export function utilityFunction() { return false; }

export default function mainFunction() { return 'foo'; }

它被第二个 ES6 模块使用:

/** /src/myModule.js **/

import mainFunction, { utilityFunction } from './dependency';

// EDIT: Fixed syntax error in code sample
// export default myModule() {
export default function myModule() {
    if (!utilityFunction()) return 2;
    return mainFunction();
}

我正在尝试使用 Jest 为 myModule.js 编写单元测试。但是当我尝试同时模拟命名导入和默认导入时,Jest 似乎只模拟命名导入。它继续使用默认导入的实际实现,并且不允许我模拟它,即使在我调用 .mockImplementation() 之后也是如此。这是我尝试使用的代码:

/** 
  * Trying to mock both named and default import. 
  * THIS DOESN'T WORK.
  */

/** /tests/myModule.test.js **/

import mainFunction, { utilityFunction } from '../src/dependency';
import myModule from '../src/myModule';

jest.mock('../src/dependency');

mainFunction.mockImplementation(() => 1);
utilityFunction.mockImplementation(() => true);

describe('myModule', () => {
    it('should return the return value of mainFunction when the result of utilityFunction is true', () => {
        expect(myModule()).toEqual(1); // FAILS - actual result is 'foo'
    });
});

这种行为对我来说真的很奇怪,因为当模拟 JUST 默认导入或 JUST 命名导入时,这个 API 工作正常。例如,在 myModule.js 仅导入默认导入的情况下,这很容易完成:

/**
  * Trying to mock just the default import. 
  * THIS WORKS.
  */

/** /src/myModule.js **/

import mainFunction from './dependency';

// EDIT: Fixed syntax error in code sample
// export default myModule() {
export default function myModule() {
    return mainFunction();
}


/** /tests/myModule.test.js **/
// If only mainFunction is used by myModule.js

import mainFunction from '../src/dependency';
import myModule from '../src/myModule';

jest.mock('../src/dependency');
mainFunction.mockImplementation(() => 1);

describe('myModule', () => {
    it('should return the return value of mainFunction', () => {
        expect(myModule()).toEqual(1); // Passes
    });
});

在仅使用命名的“utilityFunction”导出的情况下,模拟导入也很容易:

/**
  * Trying to mock both named and default import. 
  * THIS WORKS.
  */
/** /src/myModule.js **/

import { utililtyFunction } from './dependency';

// EDIT: Fixed syntax error in code sample
// export default myModule()
export default function myModule() {
    return utilityFunction();
}


/** /tests/myModule.test.js **/
// If only utilityFunction is used by myModule.js

import { utilityFunction } from '../src/dependency';
import myModule from '../src/myModule';

jest.mock('../src/dependency);
utilityFunction.mockImplementation(() => 'bar');

describe('myModule', () => {
    it('should return the return value of utilityFunction', () => {
        expect(myModule()).toEqual('bar'); // Passes
    });
});

是否可以使用 Jest 模拟命名导入和默认导入?是否可以使用不同的语法来实现我想要的结果,从模块中导入命名值和默认值并能够同时模拟它们?

最佳答案

另一个解决方案对我不起作用。我是这样做的:

  jest.mock('../src/dependency', () => ({
    __esModule: true,
    utilityFunction: 'utilityFunction',
    default: 'mainFunction'
  }));

另一种方法:

jest.unmock('../src/dependency');

const myModule = require('../src/dependency');
myModule.utilityFunction = 'your mock'

关于javascript - Jest : Mock ES6 Module with both default and named export,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48797693/

有关javascript - Jest : Mock ES6 Module with both default and named export的更多相关文章

随机推荐