Assertions

Assertions can be made from any unit test framework with a global setup.

Types and values

import { attest } from "@ark/attest"
import { type } from "arktype"

const  = type("number%2")
// asserts Even.infer is exactly number
attest<number>(.infer)
// make assertions about types and values seamlessly
attest(.infer).type.toString.snap("number")
// including object literals- no more long inline strings!
attest(.json).snap({
	domain: "number",
	divisor: 2
})

Errors

import { attest } from "@ark/attest"
import { type } from "arktype"

// Check type errors, runtime errors, or both at the same time!
// @ts-expect-error
attest(() => type("number%0")).throwsAndHasTypeError(
	"% operator must be followed by a non-zero integer literal (was 0)"
)
// @ts-expect-error
attest(() => type({ "[object]": "string" })).type.errors(
	"Indexed key definition 'object' must be a string or symbol"
)

Completions

import { attest } from "@ark/attest"
import { type } from "arktype"

// snapshot expected completions for any string literal!
// @ts-expect-error (if your expression would throw, prepend () =>)
attest(() => type({ b: "b" })).completions({
	b: ["bigint", "boolean"]
})

type  = { faker?: "🐐"; [others: string]: unknown }
// works for keys or index access as well (may need prettier-ignore to avoid removing quotes)
// prettier-ignore
attest({ "f": "🐐" } as ).completions({ "f": ["faker"] })

JSDoc

import { attest } from "@ark/attest"
import { type } from "arktype"

// match or snapshot expected jsdoc associated with the value passed to attest
const  = type({
	/** FOO */
	foo: "string"
})

const  = .assert({ foo: "foo" })

attest(.foo).jsdoc.snap("FOO")

Runtime logic

Flexibly combine runtime logic with type assertions to customize your tests beyond what is possible from pure static-analysis based type testing tools.

import { attest } from "@ark/attest"
import { type } from "arktype"
import ts from "typescript"

const  = type("<t>", "t[]")
const  = ("number | bigint")

if (ts..startsWith("5")) {
	// this assertion will only occur when testing TypeScript 5+!
	attest<(number | bigint)[]>(.infer)
}

Instantiations

Snapshot the number of type instantiations contributed by the enclosing test.

import { attest } from "@ark/attest"
import { type } from "arktype"

("discriminated union", () => {
	const  = type({
		kind: "'admin'",
		"powers?": "string[]"
	})
		.or({
			kind: "'superadmin'",
			"superpowers?": "string[]"
		})
		.or({
			kind: "'pleb'"
		})

	attest.instantiations([7574, "instantiations"])
})

On this page