I was trying to unit test a function in node which throws an error regardless of any condition. Here is my node function definition.
public testFunction() {
throw new Error('Test Error');
}
As you can see this function always throws an error whenever it has been called. I tried to execute unit testing to this function using jest .toThrow(error?) method. I was not able to unit test the function as expected.
The below mentioned are the test cases that I wrote and have attached the screenshot of the errors that I faced while executing the same.
Test Case #1
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction()).toThrow();
});
Test Case #2
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction).toThrow();
});
Test Case #3
From this blog, it was mentioned that
If we want to expect a function to throw an exception for certain input parameters, the key point is that we must pass in a function definition and not call our function inside the expect.
So I updated my test case as
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(() => testClass.testFunction()).toThrow();
});
But it was throwing error like
What is the issue with my implementation? What is the correct method to unit test a function which throws an error object back?
I was trying to unit test a function in node which throws an error regardless of any condition. Here is my node function definition.
public testFunction() {
throw new Error('Test Error');
}
As you can see this function always throws an error whenever it has been called. I tried to execute unit testing to this function using jest .toThrow(error?) method. I was not able to unit test the function as expected.
The below mentioned are the test cases that I wrote and have attached the screenshot of the errors that I faced while executing the same.
Test Case #1
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction()).toThrow();
});
Test Case #2
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(testClass.testFunction).toThrow();
});
Test Case #3
From this blog, it was mentioned that
If we want to expect a function to throw an exception for certain input parameters, the key point is that we must pass in a function definition and not call our function inside the expect.
So I updated my test case as
it('Should throw test error', (done) => {
const testClass = TestClassService.getInstance();
expect(() => testClass.testFunction()).toThrow();
});
But it was throwing error like
What is the issue with my implementation? What is the correct method to unit test a function which throws an error object back?
Share Improve this question edited Apr 18, 2020 at 18:34 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Apr 18, 2020 at 7:31 NitheeshNitheesh 20k3 gold badges27 silver badges51 bronze badges1 Answer
Reset to default 5You had it right in your third attempt. The only problem is that you are including the done
callback in the test definition. This tells the test that it is asynchronous and it expects you to call the done
callback once the test has finished.
As your test is not asynchronous, it should suffice with deleting the done
callback in the test definition:
it('Should throw test error', () => {
const testClass = TestClassService.getInstance();
expect(() => testClass.testFunction()).toThrow();
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399821a4572330.html
评论列表(0条)