Skip to content

Jest-like expect() assertions for Node.js native test runner. Zero dependencies, full TypeScript support, native mock integration.

License

Notifications You must be signed in to change notification settings

fracabu/node-test-expect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

9 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

node-test-expect

Jest-like expect assertions for Node.js native test runner

Zero dependencies, full TypeScript support

npm version Node.js TypeScript License: MIT

๐Ÿ‡ฌ๐Ÿ‡ง English | ๐Ÿ‡ฎ๐Ÿ‡น Italiano


Features

node-test-expect Features


Matchers Overview

Comprehensive Matcher Overview


๐Ÿ‡ฌ๐Ÿ‡ง English

Why node-test-expect?

Node.js v20+ has a native test runner, but:

  • Only provides basic node:assert
  • No expect() API
  • No Jest-like matchers

node-test-expect provides:

  • Zero dependencies (uses node:assert internally)
  • Full TypeScript support
  • Jest-compatible API
  • Native node:test mock integration

Install

npm install node-test-expect

Usage

import { expect } from 'node-test-expect'
import { test, mock } from 'node:test'

test('basic assertions', () => {
  expect(1 + 1).toBe(2)
  expect({ a: 1 }).toEqual({ a: 1 })
  expect('hello').toContain('ell')
})

test('mock assertions', () => {
  const fn = mock.fn()
  fn('hello')
  expect(fn).toHaveBeenCalledWith('hello')
})

test('async', async () => {
  await expect(Promise.resolve(42)).resolves.toBe(42)
})

Available Matchers

Basic: toBe, toEqual, toStrictEqual Truthiness: toBeTruthy, toBeFalsy, toBeNull, toBeUndefined Numbers: toBeGreaterThan, toBeLessThan, toBeCloseTo Strings/Arrays: toContain, toMatch, toHaveLength Objects: toHaveProperty, toMatchObject, toBeInstanceOf Mocks: toHaveBeenCalled, toHaveBeenCalledWith, toHaveBeenCalledTimes


๐Ÿ‡ฎ๐Ÿ‡น Italiano

Perche node-test-expect?

Node.js v20+ ha un test runner nativo, ma:

  • Fornisce solo node:assert base
  • Nessuna API expect()
  • Nessun matcher stile Jest

node-test-expect fornisce:

  • Zero dipendenze (usa node:assert internamente)
  • Supporto TypeScript completo
  • API compatibile Jest
  • Integrazione mock node:test nativa

Installazione

npm install node-test-expect

Utilizzo

import { expect } from 'node-test-expect'
import { test, mock } from 'node:test'

test('asserzioni base', () => {
  expect(1 + 1).toBe(2)
  expect({ a: 1 }).toEqual({ a: 1 })
  expect('ciao').toContain('ia')
})

test('asserzioni mock', () => {
  const fn = mock.fn()
  fn('ciao')
  expect(fn).toHaveBeenCalledWith('ciao')
})

Asymmetric Matchers

expect({ name: 'John', age: 30 }).toEqual({
  name: expect.any(String),
  age: expect.any(Number)
})

expect('hello world').toEqual(expect.stringContaining('world'))
expect([1, 2, 3]).toEqual(expect.arrayContaining([1, 2]))

Requirements

  • Node.js >= 20.0.0

License

MIT


Made by fracabu

About

Jest-like expect() assertions for Node.js native test runner. Zero dependencies, full TypeScript support, native mock integration.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •