
JavaScript Testing Practices and Principles
Learning Paths:
Topics:
Table of Contents
Intro to Testing
Introduction
Kent C. Dodds introduces the course as a primer for testing and testing frameworks so that students gain confidence in testing, and the ability to write tests that are maintainable. Ken also covers cloning the git repository and sets the routine for the course. Note: The repository has moved. The correct location is below.Automated Testing
Kent introduces some common bugs, what is generally done about these bugs, and gives an introduction to tests, with examples on what static, unit, integration, and a look of E2E tests.Your First Test Exercise
Kent reviews the first exercise where the audience is instructed to access the GitHub repository that was previously cloned and to write a simple test in pure JavaScript that reveals the bug and throws an error.Your First Test Solution
After walking through the solution to the Your First Test Exercise, Kent provides instructions for the next exercise, which is about writing a simple assertion library.Coding an Assertion Library
Kent writes the solution to the assertion library exercise in pure JavaScript and goes over critical issues revealed in the exercises and instructs the audience to continue to the next exercise.Coding a Testing Framework
After coding a simple testing framework from scratch in pure JavaScript, Kent introduces Jest as an example of how a testing framework can increase the productivity of the user.
Unit Tests
Jest Testing Framework
Kent provides an overview of Jest's assertion methods.Course Code Walkthrough
Kent reviews Jest and demonstrates watch mode on files in the repository.Unit Test Demo
Kent demonstrates how to write a pure function with Jest and introduces test-driven development.Writing a Basic Unit Test Exercise
Kent introduces the userToJson exercise, which is a practical look at testing a JavaScript utility function.Writing a Basic Unit Test Solution
Kent walks through the userToJson exercise in multiple ways and teaches how to use your test code to communicate intent.Test Factories & Colocating Tests Q&A
Kent fields a question that segues into test factories, describe blocks, colocating tests, Jest globals, other tools to use in addition to Jest, and other topics.Code Coverage
After introducing code coverage reports, which are a way to see what code is covered by your tests, Kent reviews its benefits as well as addressing the pitfalls of misinterpreting the data or misusing the feature.
Mocks
Monkey Patch a Mock Exercise
After explaining mocking, Kent introduces the exercise where the goal is to create a mock for a dependency in a test. The simplest form of mocking a function in a module is by using monkey patching to swap the actual implementation with a fake implementation.Monkey Path a Mock Solution
Kent codes the solution to the previous exercise by monkey patching a fake implementation of the getWinner function so that a more specific assertion can be made.Asserting Calling a Mock Exercise
In this exercise, students are to gain more confidence out of a mock by asserting that it is being called in the way that is expected.Asserting Calling a Mock Solution
After coding the solution for the mock function to keep track of how it is called and to add an assertion for it, Kent introduces the next exercise with the goal of implementing jest.spyOn.Using Jest spyOn
Kent codes the solution to Jest spyOn, which allows the user to keep track of when existing functions are called and mock their implementations.Using Jest Mock
Kent codes the solution using jest.mock, which allows the user to mock an entire module to avoid monkey patching module exports. Then Kent demonstrates another exercise that allows the user to apply the mock in almost all of the tests, rather than having it isolated.Jest Q&A
While answering a student question about the appropriateness to mock an API when the actual API is still in development, Kent is lead to the topic of discussing whether to use dependency injection or mocking.Using a __mocks__ directory
Kent walks through a test from scratch that tests the user's module. Then Kent shows the importance of being explicit in tests and discusses edge cases for this implementation.More Mocking with Users
Kent reviews an example of how to construct a test that ensures the users being retrieved from a database are what is expected.
Testing Practices
Unit Tests for Express Middleware
To test Express middleware that interacts with the database, Kent shows that one can either mock or initialize the database. In this demonstration, Kent initializes the database and asserts that the endpoints are returning the correct items from the database.Unit Tests for Express Middleware Solution
Kent live codes the solution to the Unit Tests for Express Middleware exercise, where the test checks that all posts are returning from the database, the specific post is being returned, and updatePost updates the correct post with the given changes.Test Object Factories
After demonstrating how object factories are constructed, using a previous example as scaffolding, Kent gives reviews test-cleanup.Test-Driven Development
Kent begins the section by introducing an exercise with the instructions to implement a delete function using test driven development (TDD). He then goes into further detail by outlining of the process of TDD, and in what use cases that it's a good idea to implement the practice.Test-Driven Development Solution
The solution to the exercise introduced at the beginning of Test Driven Development is live-coded.