Micro0001—Pattern: Custom Jest Error Messages
Tags: microblog, jest, tdd, react
Published: 2023-09-07 16:04:57

_Series: **<series>Micro Blogs</series>**_
_Entry: **<entry>Micro0001—Pattern: Custom Jest Error Messages</entry>**_
_First Published: **<first-published>September 2023</first-published>**_ 
_Tags:_ <tags>**<tag>microblog</tag>**, **<tag>jest</tag>**, **<tag>tdd</tag>**,  **<tag>react</tag>**</tags>

**Micro Blogs Series** – _Short posts of lessons learned, patterns, revelations, tips, missteps, etc. from the trenches of crafting software._

### <span style="color:darkred;">🔴 TL;DR</span> 

_Wrap your Jest expect statements with a try/catch block and add additional context to errors then re-throw them._ This way the test failure itself is sufficient to understand what went wrong. Example from asserting a reducer deep-copies state reference values:

```javascript
for (const [key, value] of Object.entries(reducedState)) {
  // we can ignore any non-reference values
  if (typeof value === 'object') {
    try {
      expect(reducedState[key]).not.toBe(initialState[key]);
    }
    catch(err) {
      err.message = `state.${key} —> ${err.message}`;
      throw err;
    }
  }
}
\
```

Props to [_Aart den Braber_ for the inspiration!](https://aartdenbraber.medium.com/custom-error-messages-with-jest-for-assertions-821c69e72389) Note: this test only checks one-level deep; it is not a true deep-copy.

## 🌆 Background: TDDing a Reducer

Recently I was developing a module for use with [`React.useReducer`](https://react.dev/reference/react/useReducer). I was using a few objects within my state: an array and a couple of Set’s. Long story short, as TDD was driving the design of my module, I encountered failing tests resulting from one the reference object values persisting longer than expected. If I recall correctly, one of my state manipulations ended up modifying a set that I was reusing across tests as expected data, rather than cleanly manipulating a set from a deep-copy of state as a reducer should. Oops! 🙈

I successfully found a bug in my code. What’s next? Following TDD, the first step was writing a failing test covering the situation: _test that equality (`Object.is` because that’s what React leverages) fails for any reference values within state (`typeof === 'object'`) when running the reducer._

With my test written and failing, I took the simplest path to get started—hardcoding each key that was storing a reference value. Taking the next TDD step, I looked to refactor. I realized my current implementation would require me to manually update my test and solution anytime I added a new reference value to state, and I knew future-me was _highly unlikely to remember to do so._ Refactoring, I modified my test to loop through each key-value pair and check for reference values. Now if/when I add new reference values to state my test will continue to serve its purpose! My test code ended up looking something like this:

```javascript
for (const [key, value] of Object.entries(reducedState)) {
  // we can ignore any non-reference values
  if (typeof value === 'object') {
    expect(reducedState[key]).not.toBe(initialState[key]);
  }
}
```

This worked great, but it created a new pain. When a value fails to pass the assertion, no information about its associated key is included in the error message, requiring additional work upon failure to diagnose the culprit.

## 🦓 Pattern: Custom Jest Error Messages 

I’ve wanted custom jest error messages for a while. It’s not a frequent desire, but it comes up almost every time I write assertions inside loops. So, every now and then I search the interwebz to see what advice is out there. Most frequently, I find others advising me to add an additional package that provides this functionality. Sure this works, but I try to avoid increasing the attack surface and license management burden when I can. Custom Jest error messages never felt like enough functionality to justify the additional work. Particularly when adding a simple log statement—in the infrequent event of test failure—is sufficient to track down the specific scenario.

Still… I’d rather have the test failure itself tell me exactly what was wrong, so once again I dove into the wilds of the internet. This time I found a pattern that was simple to parse and _didn’t require an entirely new package!_ Props to [_Aart den Braber_ for the inspiration!](https://aartdenbraber.medium.com/custom-error-messages-with-jest-for-assertions-821c69e72389)

The pattern: _wrap your Jest expect statements with a try/catch block and add additional context to errors then re-throw them._ Now my test now looks something like this:

```javascript
for (const [key, value] of Object.entries(reducedState)) {
  // we can ignore any non-reference values
  if (typeof value === 'object') {
    try {
      expect(reducedState[key]).not.toBe(initialState[key]);
    }
    catch(err) {
      // Pattern props to: https://aartdenbraber.medium.com/custom-error-messages-with-jest-for-assertions-821c69e72389
      err.message = `state.${key} —> ${err.message}`;
      throw err;
    }
  }
}
\
```

This way I know _exactly_ which referential value is causing the failure directly from the error message, while simultaneously retaining all the inbuilt goodness of the error jest already throws.