Function jest.mocked(source, options?)
The mocked() helper method wraps types of the source object and its deep nested members with type definitions of Jest mock function. You can pass {shallow: true} as the options argument to disable the deeply mocked behavior.
Example
import {expect, jest, test} from '@jest/globals';
import {song} from './song';
jest.mock('./song');
jest.spyOn(console, 'log');
const mockedSong = jest.mocked(song);
// or through `jest.Mocked<Source>`
// const mockedSong = song as jest.Mocked<typeof song>;
test('deep method is typed correctly', () => {
mockedSong.one.more.time.mockReturnValue(12);
expect(mockedSong.one.more.time(10)).toBe(12);
expect(mockedSong.one.more.time.mock.calls).toHaveLength(1);
});
test('direct usage', () => {
jest.mocked(console.log).mockImplementation(() => {
return;
});
console.log('one more time');
expect(jest.mocked(console.log).mock.calls).toHaveLength(1);
});
import {describe, expect, it, jest} from '@jest/globals';
import Memory from '../Memory';
import makeCalc from '../calc';
import sub from '../sub';
import sum from '../sum';
jest.mock('../Memory');
jest.mock('../sub');
jest.mock('../sum');
const mockSub = jest.mocked(sub);
const mockSum = jest.mocked(sum);
const MockMemory = jest.mocked(Memory);
describe('calc - mocks', () => {
const memory = new MockMemory();
it('returns result from subtract', () => {
mockSub.mockReturnValueOnce(0);
const calc = makeCalc(memory);
const result = calc('Sub', [2, 2]);
expect(result).toBe(0);
expect(mockSub).toHaveBeenCalledWith(2, 2);
});
it('returns result from sum', () => {
mockSum.mockReturnValueOnce(2);
const calc = makeCalc(memory);
const result = calc('Sum', [1, 1]);
expect(result).toBe(2);
expect(mockSum).toHaveBeenCalledWith(1, 1);
});
it('adds last result to memory', () => {
MockMemory.prototype.add.mockImplementationOnce(x => x);
mockSum.mockReturnValueOnce(2);
const calc = makeCalc(memory);
const sumResult = calc('Sum', [1, 1]);
const memoryResult = calc('MemoryAdd', []);
expect(sumResult).toBe(2);
expect(memoryResult).toBe(2);
expect(MockMemory.prototype.add).toHaveBeenCalledWith(2);
});
it('subtracts last result to memory', () => {
MockMemory.prototype.subtract.mockImplementationOnce(x => x);
mockSum.mockReturnValueOnce(2);
const calc = makeCalc(memory);
const sumResult = calc('Sum', [1, 1]);
const memoryResult = calc('MemorySub', []);
expect(sumResult).toBe(2);
expect(memoryResult).toBe(2);
expect(MockMemory.prototype.subtract).toHaveBeenCalledWith(2);
});
it('clears the memory', () => {
MockMemory.prototype.add.mockImplementationOnce(x => x);
mockSum.mockReturnValueOnce(2).mockReturnValueOnce(4);
const calc = makeCalc(memory);
const sumResult = calc('Sum', [1, 1]);
const memoryResult = calc('MemoryAdd', []);
const sumResult2 = calc('Sum', [2, 2]);
const clearResult = calc('MemoryClear', []);
expect(sumResult).toBe(2);
expect(memoryResult).toBe(2);
expect(sumResult2).toBe(4);
expect(clearResult).toBe(4);
expect(MockMemory.prototype.reset).toHaveBeenCalledTimes(1);
});
it('throws an error when invalid Op is passed', () => {
const calc = makeCalc(memory);
// @ts-expect-error
expect(() => calc('Multiply', [2, 3])).toThrow(new Error('Invalid op'));
});
});