小编Raj*_*ram的帖子

使用玩笑监视非导出的 node.js 函数,但未按预期工作

我试图通过“开玩笑”和“重新布线”来模拟一个非导出的函数。

在这里,我试图模拟“iAmBatman”(无双关语)但它没有被导出。

所以我使用重新布线,它工作得很好。但是 jest.mock 没有按预期工作。

我在这里遗漏了什么还是有一种简单的方法可以实现相同的目标?

jest 给出的错误信息是:

Cannot spy the property because it is not a function; undefined given instead

service.js

function iAmBatman() {
    return "Its not who I am underneath";
}

function makeACall() {
    service.someServiceCall(req => {
        iAmBatman();
    });
    return "response";
}

module.export = {
    makeACall : makeACall;
}
Run Code Online (Sandbox Code Playgroud)

jest.js

const services = require('./service');
const rewire = require('rewire');
const app = rewire('./service');
const generateDeepVoice = app.__get__('iAmBatman'); 

const mockDeepVoice = jest.spyOn(services, generateDeepVoice);

mockDeepVoice.mockImplementation(_ => {
    return "But what I …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js jestjs

5
推荐指数
1
解决办法
4268
查看次数

标签 统计

javascript ×1

jestjs ×1

node.js ×1

unit-testing ×1