I've created a service to mask data and written a Jasmine test to compare actual and expected objects.
When I run the test I get undefined on the actual object returned from the service. Although when I console log it console.log(JSON.stringify(maskedData)); the maskedData is defined.
LOG LOG: '{"handle":"#######@#####.com","displayName":"####ane","iss":"####:###.###.##.#"}'
Expected undefined to equal <jasmine.objectContaining(Object({ handle: '#######@#####.com', displayName: '####ane' }))>.
Question:
Why does service return object evaluate to undefined in a test but defined in a console log?
I did think this could be an async bug hence the undefined value during test, but since console logging the value works I don't think so.
This is a gist of the test in question:
const mockAddUserAction = {
type: 'AddUser',
payload: {
handle: '[email protected]',
displayName: 'tedJane',
iss: 'http:192.168.12.1',
},
};
it('should mask PII user action.payload data with nested properties', async () => {
const maskedData = service.maskPiiData(mockAddUserAction);
console.log(JSON.stringify(maskedData));
expect(maskedData.payload).toEqual(
jasmine.objectContaining({
handle: '#######@#####.com',
displayName: '####ane',
}),
);
});
The expect instruction is invoked before the service returns a result. You need to synchronize the asynchronous service call. Try to add await to the line where you invoke your service.
const maskedData = await service.maskPiiData(mockAddUserAction);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With