Testing

    will search in ./* and ./**/* recursively, for test files:

    • named test.{ts, tsx, js, mjs, jsx},
    • or ending with .test.{ts, tsx, js, mjs, jsx},
    • or ending with _test.{ts, tsx, js, mjs, jsx}

    To define a test you need to register it with a call to Deno.test with a name and function to be tested. There are two styles you can use.

    You can also test asynchronous code by passing a test function that returns a promise. For this you can use the async keyword when defining a function:

    1. import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
    2. Deno.test("async hello world", async () => {
    3. const x = 1 + 2;
    4. // await some async task
    5. await delay(100);
    6. if (x !== 3) {
    7. throw Error("x should be equal to 3");
    8. }

    To run the test, call deno test with the file that contains your test function. You can also omit the file name, in which case all tests in the current directory (recursively) that match the glob {*_,*.,}test.{js,mjs,ts,jsx,tsx} will be run. If you pass a directory, all files in the directory that match this glob will be run.

    1. # Run all tests in the current directory and all sub-directories
    2. deno test
    3. # Run all tests in the util directory
    4. deno test util/
    5. # Run just my_test.ts
    6. deno test my_test.ts

    deno test uses the same permission model as deno run and therefore will require, for example, --allow-write to write to the file system during testing.

    There are a number of options to filter the tests you are running.

    Tests can be run individually or in groups using the command line --filter option.

    The filter flags accept a string or a pattern as value.

    Assuming the following tests:

    1. Deno.test({ name: "my-test", fn: myTest });
    2. Deno.test({ name: "test-1", fn: test1 });

    This command will run all of these tests because they all contain the word “test”.

    To let Deno know that you want to use a pattern, wrap your filter with forward-slashes like the JavaScript syntactic sugar for a REGEX.

    Within the tests themselves, you have two options for filtering.

    Filtering out (Ignoring these tests)

    Sometimes you want to ignore tests based on some sort of condition (for example you only want a test to run on Windows). For this you can use the ignore boolean in the test definition. If it is set to true the test will be skipped.

    1. Deno.test({
    2. name: "do macOS feature",
    3. ignore: Deno.build.os !== "darwin",
    4. fn() {
    5. doMacOSFeature();
    6. },
    7. });

    Filtering in (Only run these tests)

    Sometimes you may be in the middle of a problem within a large test class and you would like to focus on just that test and ignore the rest for now. For this you can use the only option to tell the test framework to only run tests with this set to true. Multiple tests can set this option. While the test run will report on the success or failure of each test, the overall test run will always fail if any test is flagged with only, as this is a temporary measure only which disables nearly all of your tests.

    1. Deno.test({
    2. name: "Focus on this test only",
    3. only: true,
    4. fn() {
    5. testComplicatedStuff();
    6. },
    7. });

    If you have a long running test suite and wish for it to stop on the first failure, you can specify the --fail-fast flag when running the suite.

    For example integration see: