Write Functional Tests & Assertions
Today while addressing a bug in Amplify JS, I found a passing test that was broken:
expect(reject).toBeCalled();
It turns out, the implementation had a runtime error in it:
try {someUndefinedFunction();} catch (error) {// ReferenceError: someUndefinedFunction is not definedreject(error);}
But because the expect only tested that an error happened, we couldn't differentiate between an expected vs. unexpected error.
Tests should be functional: given this input, I expect that output.
// This is OK for testing that a logic gate is calledexpect(reject).toBeCalled();// 🎉 This is *better* for enforcing API contractsexpect(reject).toBeCalledWith(expectedError);
Now, our application is more resilient to regressions with:
- The logic gate for calling
reject. - The type & shape of the error passed to
reject.