Mocks
toHaveBeenCalledWithContext(context)
Use .toHaveBeenCalledWithContext to assert that a mock function was called with a specific context (this value).
const mock = jest.fn();
const context = {};
mock.call(context);
it("was called with the context", () => {
expect(mock).toHaveBeenCalledWithContext(context);
});
toHaveBeenLastCalledWithContext(context)
Use .toHaveBeenLastCalledWithContext to assert that a mock function was last called with a specific context (this value).
const mock = jest.fn();
const context = {};
mock.call({});
mock.call(context);
it("was last called with the context", () => {
expect(mock).toHaveBeenLastCalledWithContext(context);
});
toHaveBeenNthCalledWithContext(nthCall, context)
Use .toHaveBeenNthCalledWithContext to assert that a mock function was called at a specific index with a specific context (this value).
const mock = jest.fn();
const context = {};
mock.call({});
mock.call(context);
mock.call({});
it("was nth called with the context", () => {
expect(mock).toHaveBeenNthCalledWithContext(2, context);
});