模拟node.js依赖原型方法

时间:2019-05-13 04:51:07

标签: javascript node.js typescript testing jestjs

我很难用Jest(ts-jest)模拟node.js(ts-node)中的外部依赖关系。依赖性为node-chargebeehttps://github.com/chargebee/chargebee-node)。

lib将其所有终结点导出到原型类new RequestWrapper()中,与此相关的我要模拟的方法(调用chargebee API服务器的方法)为.request()(即{{1} }。

因此,RequestWrapper.prototype.request = function(callBack, envOptions)上的完整通话就像

node-chargebee

就像我说的那样,我正在尝试嘲笑chargebee.plan.retrieve("id").request((result, error) => {...}) ,而且我确信自己不知道如何做到这一点而愚蠢,并且错过了一些非常简单的东西?

简而言之,我只是想避免实际调用.request(),而忽略响应或将request()响应存根?

要多一点理解,(result, error)库的重要部分是:

node-chargebee

function createApiFunc(apiCall) { return function() { return new RequestWrapper(arguments, apiCall); }; } function RequestWrapper(args, apiCall) { this.args = args; this.httpHeaders = {}; this.apiCall = apiCall; if (this.apiCall.hasIdInUrl) { validateIdParam(this.args[0]); } } RequestWrapper.prototype.request = function(callBack, envOptions) { ... ChargeBee._core.makeApiRequest(...) } (function() { for (var res in ChargeBee._endpoints) { module.exports[res] = {}; var apiCalls = ChargeBee._endpoints[res]; for (var apiIdx = 0; apiIdx < apiCalls.length; apiIdx++) { ... module.exports[res][apiCall.methodName] = createApiFunc(apiCall); } } })(); 的示例片段如下:

ChargeBee._endpoints

0 个答案:

没有答案
相关问题