Image Credit: Dimitrij Agal

Discovering Deno — Unit testing

Geert-Jan Zwiers
Geek Culture
Published in
3 min readJun 3, 2021

--

Deno is a JavaScript runtime focused on security, reliability and productive development. It provides a great deal of testing functionality out-of-the-box. This post will take a look at getting started with unit testing in Deno and go over some of the built-ins and assertions.

Prerequisites

A Basic Test

To write a simple unit test in Deno two things are required: The built-in test runner Deno.test and an assertion, which is easily imported from the standard library’s URL:

import { assertEquals, } from "https://deno.land/std@0.204.0/assert/mod.ts";

Deno.test("example", () => {
assertEquals(1 + 2, 3);
});

Alternatively an object can be passed to Deno.testthat includes more options for test configuration. The previous test written using an object would look like this:

Deno.test({
name: "example",
fn(): {
assertEquals(1 + 2, 3);
}
});

To demonstrate testing let’s write a simple function that sums numbers. It will accept any number of arguments using rest parameters and use JavaScript’s Array.reduce function to add them all up.

export function sum(...numbers) {
return numbers.reduce((previous, current) => {
return previous + current;
})
}

Now all that is needed is to update the test code to use the new sum function. Let’s add a second test to see that it works for multiple numbers too:

import { assertEquals, } from "https://deno.land/std@0.204.0/assert/mod.ts";
import { sum } from "./sum.js"

Deno.test("sums two numbers", () => {
assertEquals(sum(1, 2), 3);
});

Deno.test("sums multiple numbers", () => {
assertEquals(sum(1, 2, 3), 6);
});

In order to run the tests you can, for example, name the file with the sum function sum.js and the file with the tests sum_test.js, then run the tests from a terminal using the deno test command, or from an editor/IDE that offers Deno support through an extension or plugin.

> deno test
running 2 tests from ./sum_test.js
sums two numbers ... ok (6ms)
sums multiple numbers ... ok (4ms)

Hopefully this shows how easy it is to get started testing code in Deno. Before moving on, a few remarks. First, you may have noticed I wrote all JavaScript code but imported assertEquals from a TypeScript (.ts) file. This is supported by Deno as well as ‘running’ TypeScript files, meaning Deno will transpile them to JavaScript without having to do that yourself or needing to install additional tools. Second, I am using pinned versions of imported modules, which is generally recommended, but if you prefer a little speed and conciseness you can also leave them out (the latest version of the module will be used, or the cached version if the module was already downloaded once before).

Hooks & Mocks

When first I wrote this post in 2021, Deno had some third-party support for more advanced testing concepts like hooks, mocks, spies and stubs. In 2023, all of these testing features have been added to the standard library and the Deno docs offer clear examples on how to use them, so I decided to simply point to some links for this (re-written) post.

Conclusion

This post covered the very basics of unit testing in Deno. Not having to worry about setting up NPM, package.json, tsconfig.json or tsnode, ES modules and so on makes testing in Deno a very pleasant experience as getting set up is truly a breeze. Deno provides everything that is needed to use test-driven development, improve code quality and refactor with confidence.

--

--