如何在 e2e jest 测试 NestJS 中使用特定服务

问题描述 投票:0回答:1

我正在尝试使用 NestJS 中的 jest e2e 测试我的登录场景。

当我登录或注册时,它会通过短信向我发送验证码,如果我硬编码短信代码或更改我的逻辑以绕过它,整个测试用例就会崩溃。

有没有办法把我的其他服务注入到我的app.e2e-spec.ts中,然后在我的测试用例中使用它来查询验证码?

我试过使用直接猫鼬连接获取验证码,但似乎是错误的

jestjs nestjs e2e-testing
1个回答
0
投票

Nest 在您编译测试应用程序时为此提供了

.overrideProvider
API。你应该能够做类似的事情

describe('E2E suite', () => {
  let app: INestApplication;
  let url: string;
  beforeAll(async () => {
    const modRef = await Test.createTestingModule({
      imports: [AppModule],
    })
    .overrideProvider(ProviderToOverride)
    .useValue(mockOfTheProvider)
    .compile();
    app = modRef.createNestApplication();
    await app.listen(0); // listen on random port
    url = await app.getUrl();
  });
  afterAll(async () => {
    await app.close();
  });
  ...tests
});
© www.soinside.com 2019 - 2024. All rights reserved.