liveBook · Manning

Test

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A documented sequence of actions and conditions that demonstrates or evaluates a specified software behavior.

Testing exercises software and checks whether its output or behavior is correct. Start by running code and inspecting the result manually. As an application grows, repeated manual checks become slow; automated tests make those checks repeatable. Automation is especially useful when many microservices must keep working after frequent changes. For an early prototype or MVP, manual testing may still be enough. section 9.4 section 9.0

Choose the scope of a test according to what you need to check:

  • Unit tests exercise a function or one aspect of it in isolation. Mock dependencies so unrelated code cannot affect the result. These tests are fast enough to run in large numbers. section 9.6
  • Integration tests exercise a larger portion of code, such as a whole microservice. Start the service and use its normal interface, including HTTP requests or messages. section 9.12
  • End-to-end tests exercise an application, or a manageable part of it, through the frontend. Docker Compose can start the application and Playwright can interact with it. section 9.12

A practical test suite usually has more unit tests than integration tests, and more integration tests than end-to-end tests. Broader tests cover more code per test but take longer to run. The right balance depends on the project and how long the team can wait for feedback. section 9.4 section 9.8.7

A first automated check

In Jest, a test named “can square two” calls square(2), then uses expect(result).toBe(4). The name states the behavior being checked, while the assertion records the expected result. For a microservice unit test, the same pattern can check a /videos handler: replace Express and MongoDB with Jest mocks, invoke the handler, and verify that it retrieves the required data. No real HTTP server or database connection is needed for that unit test. section 9.5.4 section 9.6.2

Run tests locally with npm test; a watch script can rerun them as code changes. Keep the test environment stable and, where practical, close to production. In a CI/CD pipeline, run the suite when code is pushed and use passing tests as a checkpoint before deployment. A failed suite then stops that change from reaching production. section 10.6.1 section 9.9 section 9.3

Table 9.2 Review of testing commands (view table figure)

Command

Description

npx jest --init

Initializes the Jest configuration file

npx jest

Runs tests under Jest

npx jest --watch

Runs tests with live reload enabled to rerun tests when code has changed. It uses Git to know which files have changed.

npx jest --watchAll

Same as npx jest --watch, except it monitors all files for changes and not just those that are reported changed by Git

npx playwright test

Runs Playwright tests and opens the test report in our web browser

npx playwright test

➥ --reporter=list

Runs Playwright tests, but reports results in the terminal

playwright test

➥ --workers 1

Runs Playwright tests with only a single worker. This disables parallelization, which we can’t use if we’re sharing a database between our tests.

npm test

The npm script convention for running tests. It runs Jest or Playwright (or even both if correctly configured).

This is the command to invoke in our CI/CD pipeline to execute our test suite.

npm run test:watch

This is my personal convention for running tests in live reload mode. You need to configure this script in your package.json file to use it.

Use the command table to find Jest and Playwright run and watch commands, plus the npm test convention. section 9.10

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Builds from a named Jest assertion to mocked microservice tests and outlines integration and end-to-end scope without pursuing complete coverage.
Examples
Uses a square-function assertion, mocked /videos route tests, and frontend tests against an application started with Docker Compose.
What it assumes
Assumes code and microservices to test, but starts automated testing with a simple JavaScript function before using mocks.
What you can do after
Apply it via Jest and Playwright to check code at several scopes and gate deployment through CI/CD.
How it connects
Connects unit testing, mocking, integration testing, end-to-end testing, Docker Compose, and CI/CD.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.