【发布时间】:2018-02-25 19:57:15
【问题描述】:
我正在为一个项目添加测试并提高覆盖率。我想知道如何在 NestJs 中测试模块定义(mymodule.module.ts 文件)。 特别是,我正在测试一个导入其他模块的主模块,其中一个模块启动数据库连接,这意味着我需要在另一个模块上模拟服务,以避免真正的数据库连接。 此刻我有这样的事情:
beforeEach(async () => {
const instance: express.Application = express();
module = await NestFactory.create(MyModule, instance);
});
describe('module bootstrap', () => {
it('should initialize entities successfully', async () => {
controller = module.get(MyController);
...
expect(controller instanceof MyController).toBeTruthy();
});
});
这可行,但我相信这可以得到改进:)
理想的情况是 Test.createTestingModule 提供的 overrideComponent 方法。
P.S:我使用的是 4.5.2 版本
【问题讨论】:
标签: nestjs