Skip to main content

Test

The @devicescript/test builtin package provides a lightweight unit test framework, with a subset of familiar APIs to Jest/Vitest/Mocha/Chai users (describe, test, expect).

Usage

test

Defines a test with a name and a callback. There can be many tests and the callback can be async. Tests should not be nested.

import { describe, test } from "@devicescript/test"

describe("this is a test suite", () => {
test("this is a test", async () => {})
})

describe

Declares and encapsulates a test suite. describe calls can be nested.

import { describe } from "@devicescript/test"

describe("this is a test suite", () => {})

expect

BDD style assertion API.

import { describe, test, expect } from "@devicescript/test"

describe("this is a test suite", () => {
test("this is a test", async () => {
expect(1 + 1).toBe(2)
})
})

beforeEach

Registers a callback to be called before each test in the current test suite.

import { describe, test, expect } from "@devicescript/test"

describe("this is a test suite", () => {
beforeEach(() => {
console.log(`...`)
})
test("this is a test", () => {})
})

afterEach

Registers a callback to be called after each test in the current test suite.

import { describe, test, expect } from "@devicescript/test"

describe("this is a test suite", () => {
afterEach(() => {
console.log(`...`)
})
test("this is a test", () => {})
})