Using the test Command

Learn to run and filter tests in Deno and to fail fast while running tests.

We'll cover the following

So far, we’ve learned how to run, install, and cache modules, as well as how to use permissions. As we write and run more complex programs, the need to test them starts to arise. We can do this with the test command.

Test runner

Included as part of the main binary, Deno also provides a test runner. The command for it, not surprisingly, is called test.

In this lesson, we’ll mainly explore the test command itself and not the test syntax. We’ll look at the syntax and best practices for it in more depth later in the course.

The test command finds files to run based on the {*_,*.,}test.{js,mjs,ts,jsx,tsx} glob expression.

Since glob expressions might not be too intuitive to read, we’ll explain them briefly.

It matches any files with the js, mjs, ts, jsx, and txs extensions and that have test in their name preceded by an underscore (_) or a dot (.).

A few examples of files that will match the expression and be considered tests are as follows:

  • example.test.ts
  • example_test.js
  • example.test.jsx
  • example_test.mjs

Deno tests also run inside the sandbox environment, so they need permissions. When running tests, it’s also possible to use the debugging commands we learned about earlier.

Filtering tests

A common need when we have a complete test suite is to run only a specific part of it. For this, the test command provides the --filter flag.

Imagine that we have the following file with two tests defined:

Get hands-on with 1200+ tech skills courses.