Back to Sinon

assert.calledWithNew

docs/concepts/assertions/api/called-with-new.md

22.1.01.0 KB
Original Source

assert.calledWithNew(spyOrSpyCall);

Passes, when the fake, spy or stub was called with the new operator.

It's possible to assert on a dedicated spyCall: sinon.assert.calledWithNew(call);.

js
import * as sinon from "sinon";
const fake = sinon.fake();

sinon.assert.calledWithNew(fake);
// => Uncaught Error [AssertError]: expected fake to be called with new

fake();

sinon.assert.calledWithNew(fake);
// => Uncaught Error [AssertError]: expected fake to be called with new

new fake();

// Generates no error
sinon.assert.calledWithNew(fake);

Asserting on a spyCall

js
import * as sinon from "sinon";
const fake = sinon.fake();

new fake();

// get a spyCall instance
const call = fake.firstCall;

// Generates no error
sinon.assert.calledWithNew(call);

Example using test framework

<<< ../../../.vitepress/tests/docs/assertions/api/called-with-new.test.js