javascript - 检查通过重新布线可见的(私有/非导出)函数上的方法调用
问题描述
我有一个只有一个导出函数和一些非导出函数的模块,以保持 api“干净”。我正在尝试使用 jest & require 编写单元测试,但出现以下错误:
Error: expect(jest.fn())[.not].toBeCalledTimes()
jest.fn() value must be a mock function or spy.
Received:
function: [Function doMagic]
如果我试图窥探,我怎么能开玩笑地窥探通过重新连线(或其他方法来验证方法被调用的频率)可见的非导出方法,尽管能够在最后一次测试如下所示:Cannot spy the doMagic property because it is not a function; undefined given instead
我的场景的简单示例:
例如。functions.js
const moreFunctions = require("./moreFunctions");
const theExportedFunction = someNumber => {
return doMagic(someNumber);
};
function doMagic(someNumber) {
if (someNumber % 2 === 0) {
return moreFunctions.getTrue();
}
return moreFunctions.getFalse();
}
module.exports = { theExportedFunction };
另一个模块: moreFunctions.js
const moreFunctions = {
getTrue: () => true,
getFalse: () => false
};
module.exports = moreFunctions;
我如何尝试测试它: functions.test.js
const rewire = require("rewire");
const functions = rewire("./functions");
const moreFunctions = functions.__get__('moreFunctions');
const doMagic = functions.__get__('doMagic');
const getFalse = jest.spyOn(moreFunctions, 'getFalse');
const getTrue = jest.spyOn(moreFunctions, 'getTrue');
describe("testing inner functions ", () => {
afterEach(() => {
jest.clearAllMocks();
});
test('theExportedFunction calls doMagic with 1 returns false and does not call getTrue', () => {
const result = functions.theExportedFunction(1);
console.log('result: ' + result);
expect(result).toBe(false);
//expect(doMagic).toBeCalledTimes(1); // this blows up
expect(getTrue).toHaveBeenCalledTimes(0);
expect(getFalse).toHaveBeenCalledTimes(1);
});
test('theExportedFunction calls doMagic with 2 returns true and does not call getFalse', () => {
const result = functions.theExportedFunction(2);
console.log('result: ' + result);
expect(result).toBe(true);
//expect(doMagic).toBeCalledTimes(1); // this blows up
expect(getTrue).toHaveBeenCalledTimes(1);
expect(getFalse).toHaveBeenCalledTimes(0);
});
// This works!
test('just testing to see if i can call the doMagic function', () => {
const result = doMagic(2);
expect(result).toBe(true);
expect(getTrue).toHaveBeenCalledTimes(1);
expect(getFalse).toHaveBeenCalledTimes(0);
});
});
解决方案
给定您的代码,您应该能够模拟所有并进行theExportedFunction
如下测试:
const mockGetTrue = jest.fn().mockReturnValue(true); // this name MUST start with mock prefix
const mockGetFalse = jest.fn().mockReturnValue(false); // this name MUST start with mock prefix
jest.spyOn('my_more_functions_path', () => ({
getTrue: mockGetTrue,
getFalse: mockGetFalse
}));
// here import the file you are testing, after the mocks!
// for example
import {theExportedFunction} from 'my_path';
describe('theExportedFunction', () => {
it('should call getTrue of moreFunctions', () => {
theExportedFunction(4);
expect(mockGetTrue).toHaveBeenCalled();
expect(mockGetFalse).not.toHaveBeenCalled();
});
it('should call getFalse of moreFunctions', () => {
theExportedFunction(5);
expect(mockGetFalse).toHaveBeenCalled();
expect(mockGetTrue).not.toHaveBeenCalled();
});
});
推荐阅读
- c# - InvalidAuthenticationToken - MS Graph - 一个驱动器令牌
- vue.js - Nuxt - 如何为索引页设置 BeforeEnter 保护
- reactjs - 如何使用先前创建的任务重定向到组件
- sql - 突破 case when 语句
- json - psql 无法导入包含 JSON 的 pg_dump 文件
- javascript - 如何加速 reduce 和 forEach 方法?
- r - 自动化,数据框 %>% 嵌套 %>% purrr:map / walk tidyverse 工作流,有条件地选择列
- javascript - 如何在按钮单击时重置选中的复选框?(vue.js)
- javascript - 测试引用样式组件的伪元素
- apache-flink - 将 Flink API 从 1.4 升级到 1.10