The Composition of a Test

Learn how to properly compose a Jest test.

Now that we have an understanding of the Jest setup, we can learn how to write a test.

In layman’s terms

Simply put, a test does exactly one thing. A test tells us whether Y is true if we’re given X. X can be an argument passed to a function, an API response, a state, or a sequence of user behaviors, while Y can be a return value, the calling of a function, presence in the DOM, an error, etc.

Another way of saying this is that if X, we can expect Y. For example, let’s say we have the function below:

const addNumbers = (a, b) => {
  return a + b;
}

We would reasonably expect that if 3 and 4 are passed in, we will get a returned value of 7. We can test this assumption like this:

expect(addNumbers(3,4)).toEqual(7);

Just like that, we have our first test.

The structure

Get hands-on with 1200+ tech skills courses.