Jest 模拟内部函数

IT技术 javascript jestjs
2021-01-22 19:26:15

我有一个名为helper.js 的文件,它包含两个函数

export const funcA = (key) => {
   return funcB(key)
};

export const funcB = (key,prop) => {
   return someObj;
};

我有我的helper.spec.js来测试 helper.js 文件功能。

import {funcA,funcB} from 'helper';

describe('helper', () => {
   test('testFuncB', () => {

   }
   test('testFuncA', () => {

   }
}

funcB 的测试非常简单,我只是调用它并期望someObj
问题是测试 funcA,为了测试它我想模拟 funcB 的响应。

我想要testFuncB调用实际的funcBtestFuncA调用模拟的 funcB

我怎样才能在我的两个测试中实现 funcB 被模拟和原创?

这不是重复的。这是一个不同的情况:他们只模拟内部调用的函数,如果我删除 testFuncB 那么它会是一样的,但我也必须对 testFuncB 执行测试。

6个回答

如果一个 ES6 module直接导出两个函数(不在类、对象等内,只是直接导出问题中的函数)并且一个直接调用另一个,那么该调用不能被模拟

在这种情况下,funcB 不能嘲笑funcA代码当前编写方式。

有模拟替换module出口funcB,但funcA不调用module输出funcB,它只是调用funcB直接。


惩戒funcBfuncA需要funcA调用module出口funcB

这可以通过以下两种方式之一完成:


移动funcB到它自己的module

funcB.js

export const funcB = () => {
  return 'original';
};

helper.js

import { funcB } from './funcB';

export const funcA = () => {
  return funcB();
};

helper.spec.js

import * as funcBModule from './funcB';
import { funcA } from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(funcBModule.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(funcBModule, 'funcB');
    spy.mockReturnValue('mocked');

    expect(funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});

将module导入自身

“ES6 module自动支持循环依赖”,因此它对importmodule本身完全有效,以便module内的函数可以调用module中其他函数module导出

helper.js

import * as helper from './helper';

export const funcA = () => {
  return helper.funcB();
};

export const funcB = () => {
  return 'original';
};

helper.spec.js

import * as helper from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(helper.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(helper, 'funcB');
    spy.mockReturnValue('mocked');

    expect(helper.funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});
@Prakhar在上述例子中使用exports.funcB(key)funcA
2021-03-21 19:26:15
@Prakhar 你需要用 Babel 之类的东西进行编译,因为 Node.js 中的 ES6 module支持仍然只是实验性的
2021-03-22 19:26:15
@GangafuncCCC只需要导出,funcCC需要使用module调用它。如果使用modulefuncCC调用funcCCCfuncCCC则可以在测试期间模拟module导出
2021-03-22 19:26:15
当我得到时该怎么办(function (exports, require, module, __filename, __dirname) { import
2021-03-26 19:26:15
@BrianAdams 我如何只监视 1 个嵌套的特定函数;通过第二种方法?例如:funcA 调用 {funcB, funcC, funcD,} 和 funcC 依次调用 {funcCC},后者又调用 {funcCCC},我想通过模拟 funcCCC 来为 funcA 编写测试
2021-03-30 19:26:15

迟到的答案,但这应该有效。此外,您应该在其自己的文件中而不是在“帮助程序”测试中测试 funcB。

import { funcB } from './funcB';
import { funcA } from './helper';

jest.mock('./funcB');

describe('helper', () => {
    test('test funcA', () => {
        const funcBSpy = jest.fn();
        funcB.mockImplementation(() => funcBSpy());
        
        funcA();

        expect(funcBSpy).toHaveBeenCalledTimes(1);
    });
});
如果 funcA 不是,如何调用 funcB?
2021-04-09 19:26:15
@catch22 感谢您指出 :) 更新了代码 :)
2021-04-12 19:26:15
import * as helper from 'helper';

    describe('helper', () => {
       it('should test testFuncA', () => {
          const mockTestFuncB = jest.mock();
          // spy on calls to testFuncB and respond with a mock function

           mockTestFuncB.spyOn(helper, 'testFuncB').mockReturnValue(/*your expected return value*/);

          // test logic

          // Restore helper.testFuncB to it's original function
          helper.testFuncB.mockRestore();
       }
    }

我创建了一种名称空间来处理这个问题:

let helper = {}

const funcA = (key) => {
   return helper.funcB(key)
};

const funcB = (key,prop) => {
    return someObj;
};

helper = { funcA, funcB }

module.exports = helper

然后嘲笑是明显的 jest.fn

测试时,您可以执行以下技巧funcA

1.模拟funcB

helper.funcB = jest.fn().mockImplementationOnce(() => <your data>);

2.funcB(key)更改为this.funcB(key)

我遇到了同样的问题并且工作了!完整代码:

export const funcA = (key) => {
    return this.funcB(key)
};

export const funcB = (key,prop) => {
    return someObj;
};

测试代码:

import helper from 'helper';

describe('helper', () => {
   test('testFuncB', () => {
       ...
   }
   test('testFuncA', () => {
       helper.funcB = jest.fn().mockImplementationOnce(() => <your data>);
   }
}