【问题标题】:Run supertest e2e with dynamic url for different environments using cli使用 cli 为不同环境运行具有动态 url 的 supertest e2e
【发布时间】:2022-09-28 16:06:14
【问题描述】:

我在 monorepo 中有一个 nestjs 后端 api 服务器。我想使用 supertest 进行集成测试。我目前在 package.json 中针对jest --env=node --verbose 命令配置了 e2e。 例如,检查 http://localhost:8112/health 是否返回 OK。

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

我希望 api url 端点是动态的。如果它的暂存 stg-xyz.com/health 或 prod prod-xyz.com。 我如何达到同样的效果?

  • 选项 1. 环境变量 选项 2. 从某种配置中心服务获取 API 端点

标签: jestjs nestjs e2e-testing ts-jest supertest


【解决方案1】:

创建两个单独的 ts 文件,其中指定了两个不同的环境变量,如下所示:

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

我们可以切换作为 cli 参数设置文件传递的文件

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

【讨论】:

  • 感谢与社区分享解决方案。欢迎!
猜你喜欢
  • 2019-11-29
  • 2019-11-21
  • 2018-01-01
  • 2017-04-22
  • 1970-01-01
  • 2020-07-07
  • 2017-03-05
  • 1970-01-01
  • 2017-08-30
相关资源
最近更新 更多