© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
D. IrvineBuild Your Own Test Frameworkhttps://doi.org/10.1007/978-1-4842-9247-1_5

5. Improving Legibility with Expectations and Matchers

Daniel Irvine1  
(1)
London, UK
 

In this chapter we’ll build out the expect function and its associated matcher functions. This is the Arrange phase of the test, which can be viewed as one or more expectations.

An expectation is a type of assertion about the state of the system that reads in plain English, like these examples:
expect(todo.createdAt).toBeDefined();
expect(repository.findAllMatching("")).toHaveLength(1);
expect(() => repository.add(emptyTodo())).toThrow(
  new Error("title cannot be blank")
);

The argument to expect ...

Get Build Your Own Test Framework: A Practical Guide to Writing Better Automated Tests now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.