[SOLVED] Run supertest e2e with dynamic url for different environments using cli

Issue

This Content is from Stack Overflow. Question asked by SMJ

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


This Question was asked in StackOverflow by SMJ and Answered by SMJ It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?