Testing

    Firstly, let’s create a file and register a test case using Deno.test() function.

    Secondly, run the test using deno test subcommand.

    1. $ deno test url_test.ts
    2. running 1 test from file:///dev/url_test.js
    3. test url test ... ok (2ms)
    4. test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (9ms)

    Writing tests

    To define a test you need to register it with a call to Deno.test API. There are multiple overloads of this API to allow for greatest flexibility and easy switching between the forms (eg. when you need to quickly focus a single test for debugging, using only: true option):

    1. import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
    2. // Compact form: name and function
    3. Deno.test("hello world #1", () => {
    4. const x = 1 + 2;
    5. assertEquals(x, 3);
    6. });
    7. // Compact form: named function.
    8. Deno.test(function helloWorld3() {
    9. const x = 1 + 2;
    10. assertEquals(x, 3);
    11. });
    12. // Longer form: test definition.
    13. Deno.test({
    14. name: "hello world #2",
    15. fn: () => {
    16. const x = 1 + 2;
    17. assertEquals(x, 3);
    18. },
    19. });
    20. // Similar to compact form, with additional configuration as a second argument.
    21. Deno.test("hello world #4", { permissions: { read: true } }, () => {
    22. const x = 1 + 2;
    23. assertEquals(x, 3);
    24. });
    25. // Similar to longer form, with test function as a second argument.
    26. Deno.test(
    27. { name: "hello world #5", permissions: { read: true } },
    28. () => {
    29. const x = 1 + 2;
    30. assertEquals(x, 3);
    31. },
    32. );
    33. // Similar to longer form, with a named test function as a second argument.
    34. Deno.test({ permissions: { read: true } }, function helloWorld6() {
    35. const x = 1 + 2;
    36. assertEquals(x, 3);
    37. });

    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. }
    9. });

    The test steps API provides a way to report distinct steps within a test and do setup and teardown code within that test.

    1. import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
    2. import { Client } from "https://deno.land/x/postgres@v0.15.0/mod.ts";
    3. interface User {
    4. name: string;
    5. }
    6. interface Book {
    7. id: number;
    8. title: string;
    9. }
    10. Deno.test("database", async (t) => {
    11. const client = new Client({
    12. database: "test",
    13. hostname: "localhost",
    14. port: 5432,
    15. });
    16. await client.connect();
    17. // provide a step name and function
    18. await t.step("insert user", async () => {
    19. const users = await client.queryObject<User>(
    20. "INSERT INTO users (name) VALUES ('Deno') RETURNING *",
    21. );
    22. assertEquals(users.rows.length, 1);
    23. assertEquals(users.rows[0].name, "Deno");
    24. });
    25. // or provide a test definition
    26. await t.step({
    27. name: "insert book",
    28. fn: async () => {
    29. const books = await client.queryObject<Book>(
    30. "INSERT INTO books (name) VALUES ('The Deno Manual') RETURNING *",
    31. );
    32. assertEquals(books.rows.length, 1);
    33. assertEquals(books.rows[0].title, "The Deno Manual");
    34. },
    35. ignore: false,
    36. // these default to the parent test or step's value
    37. sanitizeOps: true,
    38. sanitizeResources: true,
    39. sanitizeExit: true,
    40. });
    41. // nested steps are also supported
    42. await t.step("update and delete", async (t) => {
    43. await t.step("update", () => {
    44. // even though this test throws, the outer promise does not reject
    45. // and the next test step will run
    46. throw new Error("Fail.");
    47. });
    48. await t.step("delete", () => {
    49. // ...etc...
    50. });
    51. });
    52. // steps return a value saying if they ran or not
    53. const testRan = await t.step({
    54. name: "copy books",
    55. fn: () => {
    56. // ...etc...
    57. },
    58. ignore: true, // was ignored, so will return `false`
    59. });
    60. // steps can be run concurrently if sanitizers are disabled on sibling steps
    61. const testCases = [1, 2, 3];
    62. await Promise.all(testCases.map((testCase) =>
    63. t.step({
    64. name: `case ${testCase}`,
    65. fn: async () => {
    66. // ...etc...
    67. },
    68. sanitizeOps: false,
    69. sanitizeResources: false,
    70. sanitizeExit: false,
    71. })
    72. ));
    73. client.end();
    74. });

    Outputs:

    1. test database ...
    2. test insert user ... ok (2ms)
    3. test insert book ... ok (14ms)
    4. test update and delete ...
    5. test update ... FAILED (17ms)
    6. Error: Fail.
    7. at <stack trace omitted>
    8. test delete ... ok (19ms)
    9. FAILED (46ms)
    10. test copy books ... ignored (0ms)
    11. test case 2 ... ok (14ms)
    12. test case 3 ... ok (14ms)
    13. FAILED (111ms)

    Notes:

    1. Test steps must be awaited before the parent test/step function resolves or you will get a runtime error.
    2. Test steps cannot be run concurrently unless sanitizers on a sibling step or parent test are disabled.
    3. If nesting steps, ensure you specify a parameter for the parent step.

    Nested test steps

    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.{ts, tsx, mts, js, mjs, jsx, cjs, cts} will be run. If you pass a directory, all files in the directory that match this glob will be run.

    The glob expands to:

    • files named test.{ts, tsx, mts, js, mjs, jsx, cjs, cts},
    • or files ending with .test.{ts, tsx, mts, js, mjs, jsx, cjs, cts},
    • or files ending with _test.{ts, tsx, mts, js, mjs, jsx, cjs, cts}
    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
    1. # Pass additional arguments to the test file
    2. deno test my_test.ts -- -e --foo --bar

    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.

    1. deno help test

    Filtering

    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:

    ```ts, ignore Deno.test({ name: “my-test”, fn: myTest }); Deno.test({ name: “test-1”, fn: test1 }); Deno.test({ name: “test2”, fn: test2 });

    1. This command will run all of these tests because they all contain the word
    2. "test".
    3. ```shell
    4. deno test --filter "test" tests/

    On the flip side, the following command uses a pattern and will run the second and third tests.

    1. deno test --filter "/test-*\d/" tests/

    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.

    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. // test complicated stuff here
    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.

    1. deno test --fail-fast

    Integration with testing libraries

    For example integration see:

    Test spies are function stand-ins that are used to assert if a function’s internal behavior matches expectations. Sinon is a widely used testing library that provides test spies and can be used in Deno by importing it from a CDN, such as Skypack:

    1. import sinon from "https://cdn.skypack.dev/sinon";

    Say we have two functions, foo and bar and want to assert that bar is called during execution of foo. There are a few ways to achieve this with Sinon, one is to have function foo take another function as a parameter:

    1. // my_file.js
    2. export function bar() {/*...*/}
    3. export function foo(fn) {
    4. fn();
    5. }

    This way, we can call foo(bar) in the application code or wrap a spy function around bar and call foo(spy) in the testing code:

    ```js, ignore import sinon from ““; import { assertEquals } from “https://deno.land/std@$STD_VERSION/testing/asserts.ts“; import { bar, foo } from “./my_file.js”;

    Deno.test(“calls bar during execution of foo”, () => { // create a test spy that wraps ‘bar’ const spy = sinon.spy(bar);

    // call function ‘foo’ and pass the spy as an argument foo(spy);

    assertEquals(spy.called, true); assertEquals(spy.getCalls().length, 1); });

    1. If you prefer not to add additional parameters for testing purposes only, you
    2. can also use `sinon` to wrap a method on an object instead. In other JavaScript
    3. environments `bar` might have been accessible via a global such as `window` and
    4. callable via `sinon.spy(window, "bar")`, but in Deno this will not work and
    5. instead you can `export` an object with the functions to be tested. This means
    6. rewriting `my_file.js` to something like this:
    7. ```js
    8. // my_file.js
    9. function bar() {/*...*/}
    10. export const funcs = {
    11. bar,
    12. };
    13. // 'foo' no longer takes a parameter, but calls 'bar' from an object
    14. export function foo() {
    15. funcs.bar();
    16. }

    And then import in a test file:

    ```js, ignore import sinon from ““; import { assertEquals } from “https://deno.land/std@$STD_VERSION/testing/asserts.ts“; import { foo, funcs } from “./my_file.js”;

    Deno.test(“calls bar during execution of foo”, () => { // create a test spy that wraps ‘bar’ on the ‘funcs’ object const spy = sinon.spy(funcs, “bar”);

    assertEquals(spy.called, true); assertEquals(spy.getCalls().length, 1); }); ```