call function via closure vs call on es6 module. How to mock?
See original GitHub issueHi guys I created a stackoverflow issue but I think I can get a better answer in here.
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:
- Created 7 years ago
- Comments:11
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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
You can’t mock out exported functions - you can only mock properties on objects.
You either have to not mock
builder- or,export defaultan 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.