Skip to main content

interface Deno.DenoTest

Call Signatures #

(t: TestDefinition): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required.

import { assertEquals } from "jsr:@std/assert";

Deno.test({
  name: "example test",
  fn() {
    assertEquals("world", "world");
  },
});

Deno.test({
  name: "example ignored test",
  ignore: Deno.build.os === "windows",
  fn() {
    // This test is ignored only on Windows machines
  },
});

Deno.test({
  name: "example async test",
  async fn() {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  }
});
(
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required.

import { assertEquals } from "jsr:@std/assert";

Deno.test("My test description", () => {
  assertEquals("hello", "hello");
});

Deno.test("My async test description", async () => {
  const decoder = new TextDecoder("utf-8");
  const data = await Deno.readFile("hello_world.txt");
  assertEquals(decoder.decode(data), "Hello world");
});
(fn: (t: TestContext) => void | Promise<void>): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required. Declared function must have a name.

import { assertEquals } from "jsr:@std/assert";

Deno.test(function myTestName() {
  assertEquals("hello", "hello");
});

Deno.test(async function myOtherTestName() {
  const decoder = new TextDecoder("utf-8");
  const data = await Deno.readFile("hello_world.txt");
  assertEquals(decoder.decode(data), "Hello world");
});
(
name: string,
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required.

import { assert, fail, assertEquals } from "jsr:@std/assert";

Deno.test("My test description", { permissions: { read: true } }, (): void => {
  assertEquals("hello", "hello");
});

Deno.test("My async test description", { permissions: { read: false } }, async (): Promise<void> => {
  const decoder = new TextDecoder("utf-8");
  const data = await Deno.readFile("hello_world.txt");
  assertEquals(decoder.decode(data), "Hello world");
});
(
options: Omit<TestDefinition, "fn" | "name">,
fn: (t: TestContext) => void | Promise<void>,
): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required.

import { assertEquals } from "jsr:@std/assert";

Deno.test(
  {
    name: "My test description",
    permissions: { read: true },
  },
  () => {
    assertEquals("hello", "hello");
  },
);

Deno.test(
  {
    name: "My async test description",
    permissions: { read: false },
  },
  async () => {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  },
);
(
options: Omit<TestDefinition, "fn">,
fn: (t: TestContext) => void | Promise<void>,
): void

Register a test which will be run when deno test is used on the command line and the containing module looks like a test module.

fn can be async if required. Declared function must have a name.

import { assertEquals } from "jsr:@std/assert";

Deno.test(
  { permissions: { read: true } },
  function myTestName() {
    assertEquals("hello", "hello");
  },
);

Deno.test(
  { permissions: { read: false } },
  async function myOtherTestName() {
    const decoder = new TextDecoder("utf-8");
    const data = await Deno.readFile("hello_world.txt");
    assertEquals(decoder.decode(data), "Hello world");
  },
);

Methods #

#ignore(t: Omit<TestDefinition, "ignore">): void

Shorthand property for ignoring a particular test case.

#ignore(
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for ignoring a particular test case.

#ignore(fn: (t: TestContext) => void | Promise<void>): void

Shorthand property for ignoring a particular test case.

#ignore(
name: string,
options: Omit<TestDefinition,
"fn"
| "name"
| "ignore"
>
,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for ignoring a particular test case.

#ignore(
options: Omit<TestDefinition,
"fn"
| "name"
| "ignore"
>
,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for ignoring a particular test case.

#ignore(
options: Omit<TestDefinition, "fn" | "ignore">,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for ignoring a particular test case.

#only(t: Omit<TestDefinition, "only">): void

Shorthand property for focusing a particular test case.

#only(
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for focusing a particular test case.

#only(fn: (t: TestContext) => void | Promise<void>): void

Shorthand property for focusing a particular test case.

#only(
name: string,
options: Omit<TestDefinition,
"fn"
| "name"
| "only"
>
,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for focusing a particular test case.

#only(
options: Omit<TestDefinition,
"fn"
| "name"
| "only"
>
,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for focusing a particular test case.

#only(
options: Omit<TestDefinition, "fn" | "only">,
fn: (t: TestContext) => void | Promise<void>,
): void

Shorthand property for focusing a particular test case.

#beforeAll(fn: () => void | Promise<void>): void

Register a function to be called before all tests in the current scope.

These functions are run in FIFO order (first in, first out).

If an exception is raised during execution of this hook, the remaining beforeAll hooks will not be run.

Deno.test.beforeAll(() => {
  // Setup code that runs once before all tests
  console.log("Setting up test suite");
});
#beforeEach(fn: () => void | Promise<void>): void

Register a function to be called before each test in the current scope.

These functions are run in FIFO order (first in, first out).

If an exception is raised during execution of this hook, the remaining hooks will not be run and the currently running test case will be marked as failed.

Deno.test.beforeEach(() => {
  // Setup code that runs before each test
  console.log("Setting up test");
});
#afterEach(fn: () => void | Promise<void>): void

Register a function to be called after each test in the current scope.

These functions are run in LIFO order (last in, first out).

If an exception is raised during execution of this hook, the remaining hooks will not be run and the currently running test case will be marked as failed.

Deno.test.afterEach(() => {
  // Cleanup code that runs after each test
  console.log("Cleaning up test");
});
#afterAll(fn: () => void | Promise<void>): void

Register a function to be called after all tests in the current scope have finished running.

These functions are run in the LIFO order (last in, first out).

If an exception is raised during execution of this hook, the remaining afterAll hooks will not be run.

Deno.test.afterAll(() => {
  // Cleanup code that runs once after all tests
  console.log("Cleaning up test suite");
});

Did you find what you needed?

Privacy policy