mockImplementationOnce

fun mockImplementationOnce(implementation: F, onCall: Number = definedExternally)(source)

This function is used to change the behavior of an existing mock for a single invocation. Once invocation onCall has occurred, the mock will revert to whatever behavior it would have used had mockImplementationOnce() not been called.

The following example creates a mock function using t.mock.fn(), calls the mock function, changes the mock implementation to a different function for the next invocation, and then resumes its previous behavior.

test('changes a mock behavior once', (t) => {
let cnt = 0;

function addOne() {
cnt++;
return cnt;
}

function addTwo() {
cnt += 2;
return cnt;
}

const fn = t.mock.fn(addOne);

assert.strictEqual(fn(), 1);
fn.mock.mockImplementationOnce(addTwo);
assert.strictEqual(fn(), 3);
assert.strictEqual(fn(), 4);
});

Since

v19.1.0, v18.13.0

Parameters

implementation

The function to be used as the mock's implementation for the invocation number specified by onCall.

onCall

The invocation number that will use implementation. If the specified invocation has already occurred then an exception is thrown.