Search code examples
jestjsnestjse2e-testingts-jestsupertest

Run supertest e2e with dynamic url for different environments using cli


I have a nestjs backend api server within a monorepo. I want to do integration test using supertest. I have currently configured e2e against jest --env=node --verbose command in package.json. For example, check if http://localhost:8112/health return OK.

  it('should pass', async () => {
    const res = await request(http://localhost:8112/health)
    .get('/health')
    .expect(200);
     expect(res.text).toEqual('OK');
 });

I want the api url endpoint to be dynamic. If its staging stg-xyz.com/health or if prod prod-xyz.com. How do i achieve the same?


Solution

  • Create two separate ts files where two different environment variables are specified like below:

    process.env = Object.assign(process.env, {
      SERVICE_API_TEST_URL: 'http://localhost:8112',
      DEFAULT_TIMEOUT: 60000,
    }); 
    

    we can switch the file passing as cli agrument setupfile

    nx run api-server:e2e --setupFile=apps/api-server/e2e-test-stg-environment.ts
    
    nx run api-server:e2e --setupFile=apps/api-server/e2e-test-dev-environment.ts