Skip to content

assert.expose(target, options);

Exposes assertions into another object, to allow for integration with a test framework.

For example, sinon-chai exposes Sinon's assertions on its own object.

js
import t from "tap";
import sinon from "sinon";

t.test("assert.expose integrates assertions into another object", (t) => {
  // Create a target object to expose assertions onto
  const myAssert = {};

  // Expose sinon assertions with blank prefix
  sinon.assert.expose(myAssert, { prefix: "" });

  // Verify assertions are exposed without prefix
  t.ok(myAssert.called, "should have 'called' assertion");
  t.ok(myAssert.calledOnce, "should have 'calledOnce' assertion");
  t.ok(myAssert.calledWith, "should have 'calledWith' assertion");

  // Verify the exposed assertions work
  const fake = sinon.fake();
  fake("arg");

  t.doesNotThrow(() => myAssert.called(fake), "exposed assertion should work");

  t.end();
});

This will give you spy.should.have.been.called and so on.

See sinon-chai documentation for usage examples.

The method accepts an optional options object with two options:

prefix
Defaults to "assert", so sinon.assert.called becomes target.assertCalled. By passing a blank string, the exposed method will be `target.called`.
includeFail
true by default, copies over the `fail` and `failException` properties.