Skip to content Skip to sidebar Skip to footer

How To Test That An Inner Function Has Been Called From An Imported Function? (with Jest.js)

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 se

Solution 1:

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/

From the article:

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 () {}

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 () {}

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 () {
   returnexports.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.

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.

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

Post a Comment for "How To Test That An Inner Function Has Been Called From An Imported Function? (with Jest.js)"