TestContext

sealed external interface TestContext(source)

An instance of TestContext is passed to each test function in order to interact with the test runner. However, the TestContext constructor is not exposed as part of the API.

Since

v18.0.0, v16.17.0

Properties

Link copied to clipboard

An object containing assertion methods bound to the test context. The top-level functions from the node:assert module are exposed here for the purpose of creating test plans.

Link copied to clipboard
abstract val filePath: String?

The absolute path of the test file that created the current test. If a test file imports additional modules that generate tests, the imported tests will return the path of the root test file.

Link copied to clipboard
abstract val fullName: String

The name of the test and each of its ancestors, separated by >.

Link copied to clipboard
abstract val mock: MockTracker

Each test provides its own MockTracker instance.

Link copied to clipboard
abstract val name: String

The name of the test.

Link copied to clipboard
abstract val signal: ERROR CLASS: Symbol not found for AbortSignal
Link copied to clipboard
abstract var test: Any

This function is used to create subtests under the current test. This function behaves in the same fashion as the top level {@link test} function.

Functions

Link copied to clipboard
abstract fun after(fn: TestContextHookFn = definedExternally, options: HookOptions = definedExternally)

This function is used to create a hook that runs after the current test finishes.

Link copied to clipboard
abstract fun afterEach(fn: TestContextHookFn = definedExternally, options: HookOptions = definedExternally)

This function is used to create a hook running after each subtest of the current test.

Link copied to clipboard
abstract fun before(fn: TestContextHookFn = definedExternally, options: HookOptions = definedExternally)

This function is used to create a hook running before subtest of the current test.

Link copied to clipboard
abstract fun beforeEach(fn: TestContextHookFn = definedExternally, options: HookOptions = definedExternally)

This function is used to create a hook running before each subtest of the current test.

Link copied to clipboard
abstract fun diagnostic(message: String)

This function is used to write diagnostics to the output. Any diagnostic information is included at the end of the test's results. This function does not return a value.

Link copied to clipboard
abstract fun plan(count: Number, options: TestContextPlanOptions = definedExternally)

This function is used to set the number of assertions and subtests that are expected to run within the test. If the number of assertions and subtests that run does not match the expected count, the test will fail.

Link copied to clipboard
abstract fun runOnly(shouldRunOnlyTests: Boolean)

If shouldRunOnlyTests is truthy, the test context will only run tests that have the only option set. Otherwise, all tests are run. If Node.js was not started with the --test-only command-line option, this function is a no-op.

Link copied to clipboard
abstract fun skip(message: String = definedExternally)

This function causes the test's output to indicate the test as skipped. If message is provided, it is included in the output. Calling skip() does not terminate execution of the test function. This function does not return a value.

Link copied to clipboard
abstract fun todo(message: String = definedExternally)

This function adds a TODO directive to the test's output. If message is provided, it is included in the output. Calling todo() does not terminate execution of the test function. This function does not return a value.

Link copied to clipboard
abstract suspend fun <T> waitFor(condition: () -> ERROR CLASS: Symbol not found for Promise<T>, options: TestContextWaitForOptions = definedExternally): T
Link copied to clipboard
abstract fun <T> waitForAsync(condition: () -> ERROR CLASS: Symbol not found for Promise<T>, options: TestContextWaitForOptions = definedExternally): ERROR CLASS: Symbol not found for Promise<T>

This method polls a condition function until that function either returns successfully or the operation times out.