Testing

Discover how to write unit tests with Deno using its built-in features.

Tests ensure that our application works as expected, even in edge cases, before deploying it to production. Moreover, unit tests help keep a high-quality code level during the whole development process, detecting whether new features introduce bugs or unexpected side-effects.

Deno makes it very easy to write tests since it provides a built-in test runner. We do not need to import nor configure any external library, and we can start testing our code immediately.

Naming conventions

A test file must match one of the following conditions to be processed:

  1. Be named test.ts (e.g. ./utils/test.ts)

  2. Ending with .test. (e.g. user.test.ts)

  3. Ending with _test.ts (e.g. user_test.ts)

We can use the deno test instruction to run tests.
The command instructs the test-runner to search and execute tests in a given folder recursively. We can point to the root path of a feature or module, and the test runner will also search for tests in all nested child folders.

Test call examples:

$ deno test users/account_test.ts
// It runs only the tests in account_test.ts file

$ deno test
//runs all tests recursively inside the current directory

$ deno test utils/
//runs the tests recursively inside the utils directory

Tests formats

The code widget below gives us an overview of Deno tests. We can see the different formats available to write unit tests.

Get hands-on with 1200+ tech skills courses.