Reputation: 2946
I'm testing whether a xml2js is returning the same JS as expected. If I run the code when the expected is equal to the received, I get no errors. However, if I run the code when there are differences, I get:
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.199 s, estimated 8 s
Ran all test suites matching /create/i.
console.error
expect(received).resolves.toEqual(expected) // deep equality
So the console is able to tell me that something is not ok, but the test suite thinks everything worked well.
it('converts xml to JS', async () => {
try {
fs.readFile(__dirname + '/' + bpmnxml, 'utf8', async (err, data) => {
if (err) {
console.error(err);
} else {
try {
await expect(parseXML(data)).resolves.toEqual(bpmnjson);
} catch (errExpect) {
console.error(errExpect.message);
// eslint-disable-next-line jest/no-try-expect
// return expect(errExpect).toEqual(new Error('error on expect'));
}
}
});
} catch (errReadFile) {
console.error(errReadFile);
}
});
If I uncomment the // return expect(errExpect).toEqual(new Error('error on expect'));
then I get this other error:
(node:28424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:28424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
But the test suite still says that it passed.
What can I do to force an error (so that I get a Test Fail) but I don't get this UnhandledPromiseRejectionWarning
? The try-catch blocks are not handling this.
Upvotes: 2
Views: 1078
Reputation: 2946
It was likely related to fs.readFile
, as its synchronous version works for me:
it('converts xml to JS', async () => {
const data = fs.readFileSync(__dirname + '/' + bpmnxml, 'utf8');
expect.hasAssertions();
await expect(parseXML(data)).resolves.toEqual(bpmnjson);
});
When they should be equal:
√ converts xml to JS (16 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.069 s, estimated 4 s
When I make a random change in one of them:
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 3.247 s, estimated 4 s
Upvotes: 1