question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

call function via closure vs call on es6 module. How to mock?

See original GitHub issue

Hi guys I created a stackoverflow issue but I think I can get a better answer in here.

http://stackoverflow.com/questions/38579934/js-call-function-via-closure-vs-call-on-es6-module?noredirect=1#comment64549051_38579934

I have 2 functions defined like this:

export function builder(){ ... };
export function action() { return () => builder() };

Now I am trying to write a test that mock the builder function and return {};

 import * as m from 'redux/modules/mymodule';

 it('call buildSolrUrl', () => {
  const spy = expect.spyOn(m, "builder").andReturn({});
  m.action()();
  expect(spy.calls.length).toEqual(1);
 });

The problem is that builder does not get mock in that case. If I change my code for:

export function action() { return () => this.builder() };

The method is mocked but my program does not work anymore because action() return a function that is executed later and this is not resolved to the right object.

To resume: My code works because the call to builder() is done via closure. The tests are unable to mock the function that way because the function is called via the closure.

What is the best way to deal with this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
awei01commented, Jul 28, 2016

@casertap You’re going to have to refactor your file with exports. I believe this will work, but not tested. It’s also a bit of an ugly hack, but I don’t see any other way around it:

File with your library

const lib = {}

function doSomething(param) {
    const { useDoSomething } = lib
    return useDoSomething(param)
    // or return lib.useDoSomething(param)
}

function useDoSomething(param) {
    return param
}

lib.doSomething = doSomething
lib.useDoSomething = useDoSomething

export default lib

1reaction
ljharbcommented, Jul 26, 2016

You can’t mock out exported functions - you can only mock properties on objects.

You either have to not mock builder - or, export default an object with properties, and make sure the “action” function looks up at runtime the “builder” function on that object. Then, you can mock out that object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

js call function via closure vs call on es6 module
Now I am trying to write a test that mock the builder function and return {}; import * as m from 'redux/modules/mymodule'; ...
Read more >
ES6 Class Mocks - Jest
Calling jest.mock('./sound-player') returns a useful "automatic mock" you can use to spy on calls to the class constructor and all of its ...
Read more >
Jest — How To Mock a Function Call Inside a Module - Medium
Mocking function calls within a module. ... You can create a namespace that you export as the default object and call b using...
Read more >
Replacing JavaScript Classes With The Module Design Pattern
My name is "Adam" and I'm a JavaScript developer. ... doExternalProcessing() { // function to be called by the instantiator } ...
Read more >
Unit Testing Challenges with Modular JavaScript Patterns
The first issue is that using ES6's export syntax to share individual functions, but compiling to CommonJS, it's not possible to stub or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found