Registry / testing / assertion-error-formatter

assertion-error-formatter

JSON →
library3.0.0jsnpmunverified

Formats JavaScript Error objects into human-readable diff output (actual vs expected) for testing frameworks. Version 3.0.0 is ESM-only, released 2022-01-31, no longer supports CJS require(). Extracted from Mocha, provides customizable color functions and inline/unified diff styles. Lightweight (0 dependencies), focused on assertion error display, not assertions themselves.

npm install assertion-error-formatter
INSTALL
IMPORT
SIG · ASSERTION-ERROR-FO
A
assertion-error-formatter
testingjavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

format
import { format } from 'assertion-error-formatter'
const { format } = require('assertion-error-formatter')
Version 3 is ESM-only; CJS require() will throw. Use dynamic import if CJS is required.
default import (format)
import format from 'assertion-error-formatter'
const format = require('assertion-error-formatter')
As of v3, namespace default export is not available; use named import { format }.
type ColorFns
import type { ColorFns } from 'assertion-error-formatter'
import { ColorFns } from 'assertion-error-formatter'
Type import only for TypeScript, not a runtime value.

Shows basic usage: import format, create an error with actual/expected, call format with options, and output the diff.

import { format } from 'assertion-error-formatter'; const error = new Error('assertion failed'); error.actual = { foo: 'bar' }; error.expected = { foo: 'baz' }; const formatted = format(error, { colorFns: { diffAdded: (s) => s, diffRemoved: (s) => s, errorMessage: (s) => s, errorStack: (s) => s }, inlineDiff: false }); console.log(formatted);
Debug
Known issues
breakingVersion 3 drops CommonJS support; require() will throw MODULE_NOT_FOUND or ERR_REQUIRE_ESM.
fix
Use dynamic import() or migrate to ESM.
affects: >=3.0.0
breakingDefault export removed in v3; only named export { format } is available.
fix
Change import from 'assertion-error-formatter' to { format }.
affects: >=3.0.0
gotchaThe error object must have 'actual' and 'expected' properties; otherwise the formatted string may be empty or unhelpful.
fix
Ensure error.actual and error.expected are set before formatting.
affects: *
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Running ESM code in a CommonJS context without type:"module" or .mjs extension.
fix
Add "type": "module" to package.json or rename file to .mjs.
ERR_REQUIRE_ESM: require() of ES Module /path/to/assertion-error-formatter/index.js not supported.
Using require() to load an ESM-only package.
fix
Use dynamic import(): const { format } = await import('assertion-error-formatter');
TypeError: format is not a function
Importing as default instead of named import.
fix
Use import { format } from 'assertion-error-formatter'.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources
assertion-error-formatter — npm install assertion-error-formatter · libregistry