'Proxyquire + sinon -> stubbing Docker namespace

I can't figure out why this doesn't work. my module under test is this dockerService:

const Docker = require('dockerode');

const docker = new Docker();
  [...]
async function appDockerStart(idOrName) {
  // container ID or name
  const containers = await dockerListContainers(true);
  const myContainer = containers.find([...] using containers);
  const dockerContainer = docker.getContainer(myContainer.Id);

  await dockerContainer.start();  <=== this is what I'm trying to stub
  return `started.`;
}

my test

describe('tests', () => {
    const appName = 'website';
    let containerStub;

    beforeEach(() => {
      containerStub = {
        start: sinon.stub().returns('started'),
      };
      const dockerodeStub = {
        Container: containerStub,
      };

      dockerService = proxyquire('../../dockerService',
        {
          dockerode: dockerodeStub,
        });
    });

    afterEach(() => {
      sinon.restore();
    });

    it('should return a valid stats object', async () => {
      const res = await dockerService.appDockerStart(appName);
    });
  });

but I'm getting an error - TypeError: Docker is not a constructor it seems like the first file is failing on const docker = new Docker();

the dockerode itself a namespace which has a class Container etc etc.

How do I go about it without modifying the first file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source