Testing

Test Runner

Built-in, dependency-free test runner with describe/it/expect, nested hooks, file-level hooks, skip/only, it.each and per-test timeouts.

2 min readUpdated DocumentationEdit this page
On this page5

Usage

TypeScript
TS
1import { describe, it, expect } from '@axiljs/testing'
2
3describe('Math', () => {
4  it('adds numbers', () => {
5    expect(1 + 1).toBe(2)
6  })
7})

Nested describe() blocks prefix test names with every parent (Users > create > rejects duplicates). Also available: test (alias of it), it.skip / it.only / describe.skip / describe.only (plus xit, fit, xdescribe, fdescribe), and it.each(table)(name, fn) / describe.each(table)(name, fn).

Timeouts

Each test and hook gets 5000 ms by default (DEFAULT_TIMEOUT). Override per test with a third argument:

TypeScript
TS
it('imports a large file', async () => { /* ... */ }, 30000)

Hooks

beforeAll, afterAll, beforeEach, afterEach. Hooks are inherited: a parent's beforeEach runs before the child's, and the child's afterEach runs before the parent's. Several hooks of the same type run in registration order.

File-level hooks

Note

Fixed in 0.3.3: hooks registered outside any describe() now apply to every test in that file. They are the outermost hooks: they run before and after the describe-level hooks, and they do not leak into other test files.

TypeScript
TS
1import { beforeAll, afterAll, beforeEach, describe, it, expect } from '@axiljs/testing'
2
3let db: Database
4
5beforeAll(async () => { db = await openTestDb() })   // once for this file
6afterAll(async () => { await db.close() })
7beforeEach(async () => { await db.truncateAll() })   // before every test in this file
8
9describe('users', () => {
10  it('starts empty', async () => {
11    expect(await db.count('users')).toBe(0)
12  })
13})

Running

Terminal
❯axil test # every *.test.ts / *.spec.ts in src/ and tests/
❯axil test users # only files whose path contains "users"

Programmatically, runTests(options) resolves to true when all tests pass and runTestsWithResults(options) resolves to a RunSummary (success, passed, failed, skipped, durationMs, results). Options: timeout (default 5000) and silent (default false).

Was this page helpful?

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY