import * as dockerode from 'dockerode'; export interface TestData { networks: Dictionary; images: Dictionary; } function createMockedDockerode(data: TestData) { const mockedDockerode = dockerode.prototype; mockedDockerode.getNetwork = (id: string) => { return { inspect: async () => { return data.networks[id]; }, } as dockerode.Network; }; mockedDockerode.getImage = (name: string) => { return { inspect: async () => { return data.images[name]; }, } as dockerode.Image; }; return mockedDockerode; } export async function testWithData( data: Partial, test: () => Promise, ) { const mockedData: TestData = { ...{ networks: {}, images: {}, containers: {}, }, ...data, }; // grab the original prototype... const basePrototype = dockerode.prototype; // @ts-expect-error setting a RO property dockerode.prototype = createMockedDockerode(mockedData); try { // run the test... await test(); } finally { // reset the original prototype... // @ts-expect-error setting a RO property dockerode.prototype = basePrototype; } }