Skip to content

Instantly share code, notes, and snippets.

@pokle
Last active June 15, 2018 04:26
Show Gist options
  • Save pokle/73a7b2d9ab3537569218c60c38b01aa0 to your computer and use it in GitHub Desktop.
Save pokle/73a7b2d9ab3537569218c60c38b01aa0 to your computer and use it in GitHub Desktop.
Guard against common Jest assertion testing shortcomings
// Unfortunately there's no global unhandledResolve handler :-(
process.on('unhandledRejection', error => {
console.error('unhandledRejection', error);
throw new Error('unhandledRejection' + error);
});
beforeEach(() => expect.hasAssertions());
// 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