How to mock or test an async cb function with jest? - javascript

say I have this:
componentDidMount() {
window.Something.windowFunction(EVENT_HANDLER, this.myFunc)
}
myFunc() {
// test that this was called
}
how can I mock the window object to make sure that I get inside the myFunc
I've done this
const mockFn = jest.fn().mockImplementation((eventHandler, eventCallback) => {
return eventCallback()
})
global.Something = {
windowFunction: mockFn('event-handler', () => jest.fn()),
}
but it's never calling myFunc

Related

How do I test a function within a React hook, but that inner function returns based on the output of a promise

I have a hook that I want to test that looks something like this:
export useFunc(){
const func = async () => {
const [result, otherStuff] = await Promise.all([getSomeData(), someOtherStuff()])
if (!result){
return; // I NEED TO TEST THIS
}
return result;
}
return { func };
}
The test is set up like so:
it("whatever description", () => {
const { result } = renderHook(() => useFunc());
const { func } = result.current;
// I need to somehow test that the return gets hit inside `func`!
});
How do I go about testing this return statement?
Okay something like this works, as easy as just calling the function like Tom mentioned!
it("whatever description", async () => {
mockGetSomeData.mockResolvedValueOnce(undefined) // just need to make sure this is mocked properly
const { result } = renderHook(() => useFunc());
const { func } = result.current;
const returnedValue = await func();
expect(returnedValue).toBe(undefined);
});

Using Jest with NodeJS, Function in imported module not being mocked without using 'this' in main

In the setup below, if I run the test as is, myFunc is not mocked when I debug into handler.
However, if instead I add this. in front of the myFunc call in handler, then the function is mocked and everything works as expected.
Can someone please explain why this is? I'm new to mocking and can't see it.
I know what this does, but why won't jest mock without it since I told it to mock that function in the module?
index.js
const aws = require('aws-sdk')
exports.handler = async function (event, context) {
let s;
switch (event.func) {
case "myFunc":
console.log('Executing myFunc');
//making the call: s = await this.myFunc.apply(null, [event.params]) will make the mock work.
s = await myFunc.apply(null, [event.params])
console.log(s);
return s;
/*cases...*/
default:
// default behaviour
}
async myFunc({p1, p2}){
/* do something */
return x
}
exports.myFunc = myFunc
}
index.spec.js
jest.mock('./index.js', () => {
const allAutoMocked = jest.createMockFromModule('./index.js')
const actual = jest.requireActual('./index.js')
return {
__esModules: true,
...allAutoMocked,
myFunc : jest.fn().mockImplementation(() => ({ mockedValue: 'test' })),
handler: actual.handler
}
})
let index = require("./index.js")
describe('Test myFunc', () => {
test('If myFunc function was called', async () => {
var event = { func: 'myFunc', params: { p1: xx, p2: false } };
const context = {};
const logMock = jest.fn((...args) => console.log(...args));
const data = await handler(event, context);
})
})

mocking private methods to test an exported method fails in jest

I'm having an actual class like this
my-file.js
const methodA = () => { return 'output-from-methodA'; }
const methodB = () => { const b = methodA(); b.c = "out-put bind"; return b; }
module.exports = {
methodB
}
my-file.test.js
const { methodA, methodB } = require('./my-file.js');
describe('methodB testing', () => {
it('should call methodA', () => {
methodB();
expect(methodA).toHaveBeenCalled()
}
});
here methodA is private method, so it is not explicit to the test file, then how i ensure it is called or not in the test files
There is no way to test the private function, only the alternative way found is to test the outputs like
const { methodA, methodB } = require('./my-file.js');
describe('methodB testing', () => {
it('should call methodA', () => {
const result = methodB();
expect(result.b.c).toBe("out-put bind")
}
});

Jest Unit Testing function that calls a second one that returns a promise

Edited Question with vazsonyidl suggestions applied
I have to write unit tests for a function similar to this one:
import {External} from 'ExternalModule';
async functionA(){
this.functionB().then((data) => {
External.functionC(options);
console.log("Reached1");
}).catch((data) => {
const { OnError = "" } = data || {}
if(OnError) {
External.functionC(anotherOptions);
console.log("Reached2");
}
})
}
functionB() {
return new Promise(() => {
});
}
As functionC belongs to another module, I placed a mock of it in the _mocks_folder:
//_mocks_/ExternalModule.ts
export var External: ExternalClass = {
functionC(){}
}
class ExternalClass{
constructor(){};
functionC(){};
}
I have mocked functionB in two diferent ways for testing the then and the catch :
it("should test then block", () => {
functionB = jest.fn(() => {return Promise.resolve()});
const functionSpy = jest.spyOn(ExternalModule.External, 'functionC');
void functionA().then(() => {
expect(functionSpy).not.toHaveBeenCalled();
});
})
it("should test catch block", () => {
const err = { OnError: "Error" };
functionB = jest.fn(() => {return Promise.reject(err)});
const functionSpy = jest.spyOn(ExternalModule.External, 'functionC');
void functionA().then(() => {
expect(functionSpy).not.toHaveBeenCalled();
});
})
What I am trying to do is expect that functionC was called and called with the correct params, but the test is always passing even if I test if functionC was not called.
What am I doing wrong?
Jest does not wait for the async code to complete before doing assertions.
You can use the following function:
const waitForPromises = () => new Promise(setImmediate);
to force Jest to wait for promises to complete before continuing like so:
it("does something", async () => {
promiseCall();
await waitForPromises();
expect(something).toBe(something)
});
I think when this function catch error, this error should have an 'OnError' property so the functionC can run.
const { OnError = "" } = data || {}
if(OnError) {
ExternalClass.functionC(anotherOptions);
}
change you response error data to return Promise.reject({OnError: '404'}) may solve this problem.
Because you are not providing it to your class.
The following code is working for me:
class A {
async functionA() {
this.functionB().then((data) => {
this.functionC(); // It woll log aaa here, you need this one.
}).catch((data) => {
const {OnError = ''} = data || {};
if (OnError) {
console.log('onerror');
}
});
}
functionB() {
return new Promise(() => {
});
}
functionC() {
return 2;
}
}
describe('a', () => {
it('test', () => {
const a = new A();
a.functionB = jest.fn(() => Promise.resolve());
const functionBSpy = jest.spyOn(a, 'functionC');
void a.functionA().then(() => {
expect(functionBSpy).toHaveBeenCalledTimes(1);
});
});
});
Hope this helps, any comment appreciated.
As you provided no information about your functionB I mocked something that may suitable for you.
Your original problem is that Jest does not wait for your callbacks to settle. It does the assertion although, even if your function calls happen later, Jest will not recognise them and says that no call ever occurred.
There are several docs available, for example Jest's one here

How do I make Sinon.JS callCount increment

So I've got a Chai/Mocha/Sinon test like this:
import sinon from 'sinon'
describe(`My Test`, () => {
it(`should track the number of calls`, () => {
function testMe() {
console.log(`test me`)
}
const spy = sinon.spy(testMe)
testMe()
console.log(spy.getCalls())
console.log(spy.callCount)
})
})
When the test runs, the following is logged:
test me
[]
0
This is baffling. What am I doing wrong?
If you want spy on regular functions, the only way you can track calls to that function is by calling the spy:
it(`should track the number of calls`, () => {
function testMe() {
console.log(`test me`)
}
const spy = sinon.spy(testMe)
spy()
console.log(spy.getCalls())
console.log(spy.callCount)
})
If testMe would have been a property of an object (or a method of a class), you could call the original, because in that situation Sinon can replace the original with the spied-on version. For instance:
describe(`My Test`, () => {
it(`should track the number of calls`, () => {
const obj = {
testMe() {
console.log(`test me`)
}
};
const spy = sinon.spy(obj, 'testMe')
obj.testMe();
console.log(spy.callCount)
})
})

Categories