npm.io
2.2.1 • Published 5d ago

@logtape/testing

Licence
MIT
Version
2.2.1
Deps
0
Size
68 kB
Vulns
0
Weekly
773

Testing utilities for LogTape

JSR npm

This package provides testing utilities for LogTape. It includes a log recorder that collects LogRecord values in memory and provides matcher-based assertions for category, level, rendered message, raw message, and structured properties.

Installation

This package is available on JSR and npm. You can install it for various JavaScript runtimes and package managers:

deno add jsr:@logtape/testing  # for Deno
npm  add     @logtape/testing  # for npm
pnpm add     @logtape/testing  # for pnpm
yarn add     @logtape/testing  # for Yarn
bun  add     @logtape/testing  # for Bun

Usage

Use createLogRecorder() as a sink in tests:

import { configure, getLogger, reset } from "@logtape/logtape";
import { createLogRecorder } from "@logtape/testing";

const recorder = createLogRecorder();

try {
  await configure({
    sinks: { recorder: recorder.sink },
    loggers: [
      { category: ["my-lib"], lowestLevel: "debug", sinks: ["recorder"] },
      { category: ["logtape", "meta"], sinks: [] },
    ],
  });

  getLogger(["my-lib"]).info("User {userId} logged in.", {
    userId: 123,
  });

  recorder.assertLogged({
    category: ["my-lib"],
    level: "info",
    message: "User 123 logged in.",
    properties: { userId: 123 },
  });
} finally {
  await reset();
}

The recorder snapshots lazy callback messages when its sink receives them, so assertions see the same message a normal sink would observe at emit time. The records property returns a snapshot, and the recorder also provides clear(), take(), find(), filter(), and assertNotLogged() for tests that need lower-level access. Most property values are compared with Object.is(), Date values are compared by timestamp, and regular expression matcher values match string property values. Rendered message matching uses the same value rendering as LogTape's default text formatter.

Docs

The docs of this package is available at https://logtape.org/manual/testing. For the API references, see https://jsr.io/@logtape/testing.

Keywords