On this page

C

assert.Assert

History

The Assert class allows creating independent assertion instances with custom options.

new assert.Assert(options?): assert.Assert
Attributes
options:Object
diff:string
If set to 'full', shows the full diff in assertion errors. Defaults to 'simple'. Accepted values: 'simple', 'full'.
strict:boolean
If set to true, non-strict methods behave like their corresponding strict methods. Defaults to true.
skipPrototype:boolean
If set to true, skips prototype and constructor comparison in deep equality checks. Defaults to false.

Creates a new assertion instance. The diff option controls the verbosity of diffs in assertion error messages.

const { Assert } = require('node:assert');
const assertInstance = new Assert({ diff: 'full' });
assertInstance.deepStrictEqual({ a: 1 }, { a: 2 });
// Shows a full diff in the error message.

Important: When destructuring assertion methods from an Assert instance, the methods lose their connection to the instance's configuration options (such as diff, strict, and skipPrototype settings). The destructured methods will fall back to default behavior instead.

const myAssert = new Assert({ diff: 'full' });

// This works as expected - uses 'full' diff
myAssert.strictEqual({ a: 1 }, { b: { c: 1 } });

// This loses the 'full' diff setting - falls back to default 'simple' diff
const { strictEqual } = myAssert;
strictEqual({ a: 1 }, { b: { c: 1 } });

The skipPrototype option affects all deep equality methods:

class Foo {
  constructor(a) {
    this.a = a;
  }
}

class Bar {
  constructor(a) {
    this.a = a;
  }
}

const foo = new Foo(1);
const bar = new Bar(1);

// Default behavior - fails due to different constructors
const assert1 = new Assert();
assert1.deepStrictEqual(foo, bar); // AssertionError

// Skip prototype comparison - passes if properties are equal
const assert2 = new Assert({ skipPrototype: true });
assert2.deepStrictEqual(foo, bar); // OK

When destructured, methods lose access to the instance's this context and revert to the default assertion behavior (diff: 'simple', non-strict mode). To maintain custom options when using destructured methods, avoid destructuring and call methods directly on the instance.