如何测试是否已从导入函数调用了内部函数? (使用Jest.js) [英] How to test that an inner function has been called from an imported function? (with Jest.js)

查看:96
本文介绍了如何测试是否已从导入函数调用了内部函数? (使用Jest.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jest测试中遇到问题,即在调用外部函数之后已经调用了一个闭包(内部函数).我试过使用spyOn,没有任何积极的结果.这似乎是一个相对简单的问题,但我没有从Google搜索中找到任何结果.

I'm having issues with Jest testing that a closure (an inner function) has been called after calling the outer function. I've tried using the spyOn with no positive result. This seems to be a relatively simple problem, to which I haven't found any results from googling.

// helper.js
export const bar = () => {}
export const foo = () => {
  bar(); //How do I test that this has been called?
}


//helper.test.js
import * as H from 'helper';

const barSpy = jest.spyOn(H, 'bar');
H.foo();

expect(barSpy).toHaveBeenCalled(); // Jest throws error "Expected mock function to have been called." 

推荐答案

我曾经遇到过同样的问题,并且发现这篇文章的解释非常清楚:

I had the same issue once, and found this article which is very well explained: https://www.exratione.com/2015/12/es6-use-of-import-property-from-module-is-not-a-great-plan/

摘自文章:

在Javascript中对函数进行存根要求将函数绑定到上下文(任何上下文),该上下文在测试代码和被测试代码的范围内.在理智的世界中,此上下文由模块提供.例如,在ES5 Node.js中:

Stubbing a function in Javascript requires the function to be bound to a context, any context, that is in scope for both the test code and the code being tested. In a sane world this context is provided by the module. For example, in ES5 Node.js:

exports.fn = function () {}

以下是在ES5中要避免的事情,使用函数覆盖module.exports.太多的人这样做,这是不明智的,因为使用该代码的任何模块都必须采取额外的步骤来进行有用的单元测试:

The thing to avoid doing in ES5 is the following, overriding module.exports with a function. All too many people do this and it is inconsiderate, as any module using that code must then take extra steps to be usefully unit tested:

module.exports = function () {}

所以最后一个导出函数不能被存根,因为它没有绑定到任何上下文,为此存在一个工作方法,将函数绑定到要测试的模块的exports对象,并将其称为exports.importedFunction,就像这样:

So the last exported function can't be stubbed since it's not bound to any context, theres a workaraound for that, bounding the function to the exports object of the module you are testing and calling it as exports.importedFunction, like so:

var example = require('./example');

// Exported for test purposes. If we don't do this, then
// example is encapsulated here and cannot be stubbed.
exports.example = example;

// Usage.
exports.invokeExample = function () {
   return exports.example();
};

然后,您将可以对其进行监视,但是您必须编写该额外的代码,这有点丑陋,而且也不是很有用.

Then you'll be able to spy on it, but you had to write that extra code, which is kinda ugly and not very useful neither clear.

在ES6中,使用从'y'导入{x}"与覆盖module.exports类似,在ES5中具有功能.结果是导入的函数封装在模块中,并且在不编写更多样板代码的情况下无法在单元测试中进行存根.

In ES6 using "import { x } from 'y'" is analogous to overwriting module.exports with a function in ES5. The result is that an imported function is encapsulated in the module and cannot be stubbed in unit tests without writing more boilerplate code that would otherwise have been the case.

对于您的情况,也会发生这种情况:import * as H from 'helper'

This also happens for your case: import * as H from 'helper'

这篇关于如何测试是否已从导入函数调用了内部函数? (使用Jest.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆