Avoid using toBeTruthy() and toBeFalsy() matchers

Test assertions should be explicit. If I’m trying to assert that something should be true I avoid using the toBeTruthy() matcher. It’s important to realize that in JavaScript, all values are truthy unless they are defined as falsy. This means that as long as result is not a falsy value (e.g. false, "", null, undefined, NaN, etc.), the assertion will pass. expect(result).toBeTruthy() // passes if result is {} or [] or 1 Contrast it with this approach where result will only pass if its value is true....

1 min · Jesstern Rays

How to structure your unit tests

When writing test cases, we can organize our tests into three segments Arrange – set variables, stubs, mocks, spies, etc Act – the action that you wish to assert on Assert – make an assertion on the expected and actual results Here’s a simple example: test("renders correct number of books", () => { // arrange const books = [ {title: "Animal Farm", author: "George Orwell"}, {title: "Nineteen Eighty-Four", author: "George Orwell"}, {title: "Down and Out in Paris and London", author: "George Orwell"}, ] // act const { queryAllByTestId } = render(<BookList books={books} />); // assert expect(queryAllByTestId('book-item')....

1 min · Jesstern Rays

Run Jest tests that match a describe block

How to run tests that only match the name in the describe block

1 min · Jesstern Rays

Watch for file changes as you run your tests

How to run your tests as your files change in Jest

1 min · Jesstern Rays