Improve Your Testing #8: Keeping Your Tests Short and Clean
Do you think that tutorials only show toy examples far from your daily challenges? Just check this article.
In the previous article, I had shown how you can improve the quality of tests using AAA. Very nice, but I can already hear the criticism. The example tests were so simple that they are unrealistic, right? Just look at the following test:
let notebook;
beforeEach(() => {
notebook = new Notebook(); // Arrange
});
test("add contact", () => {
notebook.add("John", 1234); // Act
expect(notebook.get("John")).toBe(1234); // Assert
});
test("update contact", () => {
notebook.add("John", 1234); // Arrange
notebook.update("John", 3456); // Act
expect(notebook.get("John")).toBe(3456); // Assert
});
test("delete contact", () => {
notebook.add("John", 1234); // Arrange
notebook.delete("John"); // Act
expect(notebook.get("John")).toBeNull(); // Assert
});
Have you seen it? It’s simple! It’s so simple that it seems far from the complexity of the real world. Right? Well, what if I told you that you can transform any complicated test into a test as simple as this one? How? That’s what we’ll look at now. And the result? It will surprise…