Last active
June 15, 2018 04:26
-
-
Save pokle/73a7b2d9ab3537569218c60c38b01aa0 to your computer and use it in GitHub Desktop.
Guard against common Jest assertion testing shortcomings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unfortunately there's no global unhandledResolve handler :-( | |
process.on('unhandledRejection', error => { | |
console.error('unhandledRejection', error); | |
throw new Error('unhandledRejection' + error); | |
}); | |
beforeEach(() => expect.hasAssertions()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Try running this with and without the following require: | |
require('./jest-guard') | |
test("I want this to fail, because I've forgotten to expect() anything", () => {}); | |
test("I want this to fail because I haven't returned the expectation of a promise, and the assertion false", () => { | |
expect(Promise.resolve(true)).resolves.toBe(false); | |
}); | |
// Don't have a solution for these yet :-( | |
test("I want this to fail because I haven't returned the expectation of a promise even though the assertion passes", () => { | |
expect(Promise.resolve(true)).resolves.toBe(true); | |
}); | |
function neverResolveOrReject() { | |
return new Promise((resolve, reject) => {}); | |
} | |
test("I want this to fail because the promise never resolves or rejects - but it passes!!!", () => { | |
expect(neverResolveOrReject()).resolves.toBe("something"); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment