Write Functional Tests & Assertions

Today while addressing a bug in Amplify JS, I found a passing test that was broken:


It turns out, the implementation had a runtime error in it:

try {
} catch (error) {
  // ReferenceError: someUndefinedFunction is not defined

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 called
// 🎉 This is *better* for enforcing API contracts

Now, our application is more resilient to regressions with:

  1. The logic gate for calling reject.
  2. The type & shape of the error passed to reject.