![](/img/trans.png)
[英]jest+supertest e2e testing of API endpoints that issue HTTP requests to other endpoints
[英]Run supertest e2e with dynamic url for different environments using cli
我在 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。 我如何達到同樣的效果?
創建兩個單獨的 ts 文件,其中指定了兩個不同的環境變量,如下所示:
process.env = Object.assign(process.env, {
TOKEN_SERVICE_API_TEST_URL: 'http://localhost:8112',
DEFAULT_TIMEOUT: 60000,
});
我們可以切換作為 cli agrument setupfile 傳遞的文件
nx run token-api-server:e2e --setupFile=apps/token-api-server/e2e-test-stg-environment.ts
nx run token-api-server:e2e --setupFile=apps/token-api-server/e2e-test-dev-environment.ts
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.