How to trace a failed asynchronous test in Jest? - javascript

- UPDATE -
The issue has been identified.
In the actual codebase the assertion is passed to a imported callback, and once the callback executes with a failed test, it raises a promise rejection.
So, this is close to how the test was actually written:
describe( "file system", () => {
it( "should check if the file exists", async () => {
call( async () => {
const received = await fileExists();
const expected = true;
expect( received ).toBe( expected );
});
});
});
and the complex callback is presented in a simpler way to produce the same issue:
export function call( callback) {
callback();
}
- UPDATE -
The following code works.
I picked up a small portion of the code from a large codebase for better visibility. If I run just the following piece of code, it works as expected. I think there's an issue in the actual codebase.
#Flask's recommendation of handling the unhandled promise rejections centrally added a great value to the question.
Consider the following test:
import fileExists, { call } from "./exists";
describe( "file system", () => {
it( "should check if the file exists", async () => {
const received = await fileExists();
const expected = true;
expect( received ).toBe( expected );
});
});
for the following source:
import fs, { constants } from "fs";
import { promisify } from "util";
export default async function fileExists() {
const path = ".nonexistent";
const access = promisify( fs.access );
try {
await access( path, constants.F_OK );
} catch {
return false;
}
return true;
}
When fileExists rejects returns false, an UnhandledPromiseRejectionWarning is received as expected. But this does not help trace the source of the failed test.
For synchronous tests, Jest shows the path to the test (i.e. file system › should check if the file exists) which helps trace the source of the failed test.
What is the best way to achieve this for asynchronous tests?

UnhandledPromiseRejectionWarning is not expected here. It's not equivalent to failed test because it doesn't prevent a test to pass if assertions pass. It means that the code was written the wrong way and contains unchained promises.
It can happen only if await was omitted in the test:
fileExists(); // no await
Or fileExists function contains loose unhandled promises:
fileExists() {
whatever() // no return
.then(() => {
whatever() // no return
}) // no catch to suppress errors
}
It's a good practice to have in setupFiles:
process.on('unhandledRejection', console.error);
It provides more useful output than UnhandledPromiseRejectionWarning and allows to debug the problem based on error stack.

Related

Write jest unit testcases for exception handling getting issue on while run

This is my controller class:
#Put()
async updateUser(#BodyToClass() user: UpsertUserDto): Promise<UpsertUserDto> {
try {
return await this.userService.updateUser(user);
} catch (e) {
throw new HttpException(e.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
This is my Usercontroller.spec test class, I am writing the testcases for exception handling for negative cases.
While running getting error on this line:
".then(() => done.fail('Client controller should return INTERNAL_SERVER_ERROR 500 '))
Here is my failure message from Jest:
at Env.fail (../node_modules/jest-jasmine2/build/jasmine/Env.js:722:61) at user/user.controller.spec.ts:51:33 at Object. (user/user.controller.spec.ts:50:15)
I am not sure where I am doing mistake or whether there is other way to handle the exception in nestjs.
it('should throw INTERNAL_SERVER_ERROR if user not update', async (done) => {
const expectedResult = undefined;
const testuser = new UpsertUserDto();
testuser.id = '123';
jest.spyOn(userservice, 'updateUser').mockResolvedValue(expectedResult);
await usercontroller.updateUser(testuser)
.then(() => done.fail('Client controller should return INTERNAL_SERVER_ERROR 500 '))
.catch((error) => {
expect(error.status).toBe(500);
expect(error.message).toMatchObject({error: ' INTERNAL_SERVER_ERROR', statusCode: 500});
done();
});
});
You're using mockResolvedValue in your jest.spyOn which means that in a .then chain, the .catch will never be executed. Looking at your code, all that's happening it your UserService method returns undefined and as there is no logic around it, the controller then returns that undefined meaning there's no error to be caught. If you are trying to test an error, you should be using mockRejectedValue instead. Also, it's usually not the best practice to mix using async/await with using a done callback, as it can lead to some weird situations.
And lastly, Jest has a built in way to check for errors from a function. Your test could look something like this instead:
it('should throw INTERNAL_SERVER_ERROR if user not update', async () => {
const testuser = new UpsertUserDto();
testuser.id = '123';
jest.spyOn(userservice, 'updateUser').mockRejectedValue(new Error('There was an error'));
await expect(usercontroller.updateUser(testuser)).rejects.toThrow(HttpException);
await expect(usercontroller.updateUser(testuser)).rejects.toThrow('There was an error');
});
I suggest you spend some time reading Jest's documentation and possibly looking at some examples to get your feet on the ground with testing.

What is considered the correct way to test methods that return http observables?

I have a TypeScript project which I would like to deploy as JS NPM package. This package performs some http requests using rxjs ajax functions. Now I would like to write tests for these methods.
At some point I have a method like this (simplified!):
getAllUsers(): Observable<AjaxResponse> {
return ajax.get(this.apiUrl + '/users');
}
I know about basic testing, for example with spyOn I can mock a response from the server. But how would I actually test the http request?
The documentation of jasmine says that I cannot do async work in the it part, but in the beforeEach: https://jasmine.github.io/tutorials/async
Would this be the correct approach to test the API?
let value: AjaxResponse;
let error: AjaxError;
beforeEach((done) => {
const user = new UsersApi();
user.getAllUsers().subscribe(
(_value: any) => {
value = _value;
done();
},
(_error: any) => {
error = _error;
done();
}
);
});
it("should test the actual http request", () => {
// Test here something
// expect(value).toBe...
// expect(error).toBe...
});
I couldn't think of another approach how to do the async work...
You need to mock ajax.get to return an Observable that emits values that you want to test.
This is done depending on how ajax is declared in your file that contains user.getAllUsers method.
It'd be ideal if UsersApi() had ajax passed into it (pure function style) because then you could just do something like this:
e.g.
class UsersApi {
public ajax;
constructor(ajax) {
this.ajax = ajax;
}
getAllUsers() {
return this.ajax.get(....)
}
}
Edit: Passing in dependencies (aka dependency injection) is one thing that makes modules like this significantly easier to test - consider doing it!
Then you could very easily mock your tests out like this:
const someSuccessfulResponse = ...
const someFailedResponse = ...
const ajaxWithSuccess = {
get:jest.fn(() => of(someSuccessfulResponse))
}
const ajaxWithFailed = {
get:jest.fn(() => throwError(someFailedResponse))
}
describe('my test suite',() => {
it("should test a successful response", (done) => {
const user = new UsersApi(ajaxWithSuccess);
user.getAllUsers().subscribe(d => {
expect(d).toBe(someSuccessfulResponse);
done();
});
});
it("should test a failed response", (done) => {
const user = new UsersApi(ajaxWithFailed);
user.getAllUsers().subscribe(null,error => {
expect(d).toBe(someFailedResponse);
done();
});
});
});
Note: You DO NOT want to test the actual API request. You want to test that your code successfully handles whatever API responses you think it could receive. Think about it, how are you going to test if a failed API response is handled correctly by your code if your API always returns 200s?
EDIT #27: The above code works fine for me when I run jest, not totally clear on why jasmine (doesn't jest run on jasmine?) says it can't do async in it's. In any case, you could just change the code above to set everything up in the beforeEach and just do your expects in the it's.

Jest Unit test, mock implementation of IF condition within function for complete code coverage

I have a API script in a file
const ApiCall = {
fetchData: async (url) => {
const result = await fetch(url);
if (!result.ok) {
const body = await result.text(); // uncovered line
throw new Error(`Error fetching ${url}: ${result.status} ${result.statusText} - ${body}`); // uncovered line
}
return result.json();
},
};
export default ApiCall;
When I mock the call, I have two uncovered lines in code coverage.
Any idea how can I make them cover as well.
Here is what I have tried so far which is not working
it('test', async () => {
ApiCall.fetchData = jest.fn();
ApiCall.fetchData.result = { ok: false };
});
I am kind of new into Jest, so any help would be great.
You need to provide a stubb response in your test spec so that the if statement is triggered. https://www.npmjs.com/package/jest-fetch-mock will allow you to do just that. The example on their npm page should give you what you need https://www.npmjs.com/package/jest-fetch-mock#example-1---mocking-all-fetches
Basically the result is stored in state(redux) and is called from there. jest-fetch-mock overrides your api call/route and returns the stored result in redux all within the framework.
Assuming that what you want to test is the ApiCall then you would need to mock fetch. You are mocking the entire ApiCall so those lines will never execute.
Also, you have an issue, because if you find an error or promise rejection, the json() won't be available so that line will trigger an error.
Try this (haven't test it):
it('test error', (done) => {
let promise = Promise.reject(new Error("test"));
global.fetch = jest.fn(() => promise); //You might need to store the original fetch before swapping this
ApiCall.fetchData()
.catch(err => );
expect(err.message).toEqual("test");
done();
});
it('test OK', (done) => {
let promise = Promise.resolve({
json: jest.fn(() => {data: "data"})
});
global.fetch = jest.fn(() => promise);
ApiCall.fetchData()
.then(response => );
expect(response.data).toEqual("data");
done();
});
That probably won't work right away but hopefully you will get the idea. In this case, you already are working with a promise so see that I added the done() callback in the test, so you can tell jest you finished processing. There is another way to also make jest wait for the promise which is something like "return promise.then()".
Plese post back

Testing async/await method. Exception not being caught by Jest in unit test

I'm attempting to test some code that uses await and async using Jest. The problem I'm having is an exception is thrown, but Jest doesn't seem to catch it.
For example, here is run method that checks to see if session.url is present. If not, an exception is thrown:
const args = require('args');
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
// Loads JSON configuration file
module.exports.loadConfigFile = async (filename) => {
const contents = await readFile(filename, 'utf8');
return JSON.parse(contents);
};
// Prepare session ensuring command line flags override config
module.exports.prepareSession = async (flags) => {
if(!flags.config) return flags;
const config = await this.loadConfigFile(flags.config);
return {...config, ...flags};
};
// Runs the race application
module.exports.run = async (flags = {}) => {
const session = await this.prepareSession(flags);
if(!session.url) throw new Error('A valid URL is required');
};
Here I test to see if an exception is thrown:
describe('Race Module', () => {
test('Throws exception if no URL is provided', async () => {
const runner = await race.run();
expect(runner).toThrowError();
});
...
The test runs and it appears an exception is thrown but jest hasn't caught it and it doesn't pass:
Race Module
✕ Throws exception if no URL is provided (3ms)
● Race Module › Throws exception if no URL is provided
A valid URL is required
at Object.<anonymous>.module.exports.run (src/race.js:23:27)
at <anonymous>
Any ideas where I'm going wrong?
My initial thought was to chain catch(() => {}) to race.run() in the test but I am not entirely sure how I would test that. That might not even be the problem.
The fix was to use rejects.toThrow. But, note that this functionality is currently broken in master. I had to use a beta branch. See here: https://github.com/facebook/jest/pull/4884
test('Throws exception if no URL is provided', async () => {
await expect(race.run())
.rejects
.toThrow('A valid URL is required');
});

Can you write async tests that expect toThrow?

I'm writing an async test that expects the async function to throw like this:
it("expects to have failed", async () => {
let getBadResults = async () => {
await failingAsyncTest()
}
expect(await getBadResults()).toThrow()
})
But jest is just failing instead of passing the test:
FAIL src/failing-test.spec.js
● expects to have failed
Failed: I should fail!
If I rewrite the test to looks like this:
expect(async () => {
await failingAsyncTest()
}).toThrow()
I get this error instead of a passing test:
expect(function).toThrow(undefined)
Expected the function to throw an error.
But it didn't throw anything.
You can test your async function like this:
it('should test async errors', async () => {
await expect(failingAsyncTest())
.rejects
.toThrow('I should fail');
});
'I should fail' string will match any part of the error thrown.
I'd like to just add on to this and say that the function you're testing must throw an actual Error object throw new Error(...). Jest does not seem to recognize if you just throw an expression like throw 'An error occurred!'.
await expect(async () => {
await someAsyncFunction(someParams);
}).rejects.toThrowError("Some error message");
We must wrap the code in a function to catch the error. Here we are expecting the Error message thrown from someAsyncFunction should be equal to "Some error message". We can call the exception handler also
await expect(async () => {
await someAsyncFunction(someParams);
}).rejects.toThrowError(new InvalidArgumentError("Some error message"));
Read more https://jestjs.io/docs/expect#tothrowerror
Custom Error Class
The use of rejects.toThrow will not work for you. Instead, you can combine the rejects method with the toBeInstanceOf matcher to match the custom error that has been thrown.
Example
it("should test async errors", async () => {
await expect(asyncFunctionWithCustomError()).rejects.toBeInstanceOf(
CustomError
)
})
To be able to make many tests conditions without having to resolve the promise every time, this will also work:
it('throws an error when it is not possible to create an user', async () => {
const throwingFunction = () => createUser(createUserPayload)
// This is what prevents the test to succeed when the promise is resolved and not rejected
expect.assertions(3)
await throwingFunction().catch(error => {
expect(error).toBeInstanceOf(Error)
expect(error.message).toMatch(new RegExp('Could not create user'))
expect(error).toMatchObject({
details: new RegExp('Invalid payload provided'),
})
})
})
I've been testing for Firebase cloud functions and this is what I came up with:
test("It should test async on failing cloud functions calls", async () => {
await expect(async ()=> {
await failingCloudFunction(params)
})
.rejects
.toThrow("Invalid type"); // This is the value for my specific error
});
This is built on top of lisandro's answer.
If you want to test that an async function does NOT throw:
it('async function does not throw', async () => {
await expect(hopefullyDoesntThrow()).resolves.not.toThrow();
});
The above test will pass regardless of the value returned, even if undefined.
Keep in mind that if an async function throws an Error, its really coming back as a Promise Rejection in Node, not an error (thats why if you don't have try/catch blocks you will get an UnhandledPromiseRejectionWarning, slightly different than an error). So, like others have said, that is why you use either:
.rejects and .resolves methods, or a
try/catch block within your tests.
Reference:
https://jestjs.io/docs/asynchronous#asyncawait
This worked for me
it("expects to have failed", async () => {
let getBadResults = async () => {
await failingAsyncTest()
}
expect(getBadResults()).reject.toMatch('foo')
// or in my case
expect(getBadResults()).reject.toMatchObject({ message: 'foo' })
})
You can do like below if you want to use the try/catch method inside the test case.
test("some test case name with success", async () => {
let response = null;
let failure = null;
// Before calling the method, make sure someAsyncFunction should be succeeded
try {
response = await someAsyncFunction();
} catch(err) {
error = err;
}
expect(response).toEqual(SOME_MOCK_RESPONSE)
expect(error).toBeNull();
})
test("some test case name with failure", async () => {
let response = null;
let error = null;
// Before calling the method, make sure someAsyncFunction should throw some error by mocking in proper way
try {
response = await someAsyncFunction();
} catch(err) {
error = err;
}
expect(response).toBeNull();
expect(error).toEqual(YOUR_MOCK_ERROR)
})
Edit:
As my given solution is not taking the advantage of inbuilt jest tests with the throwing feature, please do follow the other solution suggested by #Lisandro https://stackoverflow.com/a/47887098/8988448
it('should test async errors', async () => {
await expect(failingAsyncTest())
.rejects
.toThrow('I should fail');
});
test("It should test async on failing cloud functions calls", async () => {
failingCloudFunction(params).catch(e => {
expect(e.message).toBe('Invalid type')
})
});

Categories