我正在尝试使用 NestJS 中的 jest e2e 测试我的登录场景。
当我登录或注册时,它会通过短信向我发送验证码,如果我硬编码短信代码或更改我的逻辑以绕过它,整个测试用例就会崩溃。
有没有办法把我的其他服务注入到我的app.e2e-spec.ts中,然后在我的测试用例中使用它来查询验证码?
我试过使用直接猫鼬连接获取验证码,但似乎是错误的
.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
});