Home > Net >  No Docker client strategy found
No Docker client strategy found

Time:10-07

I'm trying to implement E2E testing in NestJS application. I'm using Postgres in a docker container. It is running fine for other testing. The only problem is E2E testing. And I'm getting the following error:

    No Docker client strategy found

      at node_modules/testcontainers/dist/docker/docker-client.js:46:11
      at fulfilled (node_modules/testcontainers/dist/docker/docker-client.js:5:58)


  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'close')

      59 |
      60 |   afterAll(async () => {
    > 61 |     await app.close();
         |               ^
      62 |     await container.stop();
      63 |   });
      64 |

      at Object.<anonymous> (test/event.e2e-spec.ts:61:15)

My code is given below:

describe('EventAPI (e2e)', () => {
  jest.setTimeout(180_000);
  let app: INestApplication;
  let service: Service;
  let container: E2EPgContainer;

  beforeAll(async () => {
    container = new E2EPgContainer();
    await container.init();

    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const AppModule = require('../src/server/app/app.module').AppModule;
    const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = module.createNestApplication();
    app.useGlobalPipes(new ValidationPipe({ transform: true }));
    service = module.get<Service>(Service);
    await app.init();
  });
  afterEach(() => {
    jest.clearAllMocks();
  });

  afterAll(async () => {
    await app.close();
    await container.stop();
  });

  describe('GET-POINT API', () => {
     it('GET /:customerId --> should return success 200 after get points', async () => {
        // test code
     });
  });
});

How can I solve this issue?

CodePudding user response:

I'm assume that your docker container is running, but not as rootless. Make sure you are running your docker as rootless. You can try following:

$ sudo groupadd docker
$ sudo gpasswd -a $USER docker

Now make sure the following environment variables are set (or add them to ~/.bashrc)

export PATH=/home/ubuntu/bin:$PATH
export DOCKER_HOST=unix:///run/user/1001/docker.sock

Now try to restart docker

$ sudo service docker restart

After all steps try following docker command in terminal without sudo:

$ docker ps

If you are able to see your docker container, then it will solve your problem.

  • Related