"It" 声明继续使用常规数据库而不是测试数据库

"It" statement keep using regular database instead of the test database

我正在做一些测试,我只想测试我的端点。什么进什么出就是这样。

我有一个测试数据库,想在测试时使用运行。

在 "BeforeAll" 中,我连接到测试数据库,在我的 beforeach 中,我创建了一个 post 用户。 它工作它被插入到测试数据库

问题是当我尝试在 "It" 语句中发出请求时,使用的数据库是 app one 而不是测试数据库 :/

beforeAll(async () => {
    await mongoose.connect(testDatabase);
});
afterAll(async function () {
    await mongoose.disconnect()
});

describe('/user', () => {
    let app;

    beforeEach(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule, UserModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        await app.init();

        // this send correctly data to the TEST database
enter code here
        return request(app.getHttpServer()) 
            .post('/user').send(TEST_USER)
            .set('Accept', 'application/json')
            .expect(201)
            .then(r => console.log("Result of post", r.body))
    });

    it('GET', () => {
        // my probleme here : this retrieve the regular database content (setup in main file) NOT THE TEST database setup in beforeAll
        return request(app)
            .get('/user')
            .expect(200)
            .then(r => console.log("Result of get", r.body))
    });
});

我做错了吗? 谢谢你们!

我敢打赌你的应用程序没有使用你在那里建立的连接 - 你的应用程序在请求期间很可能会创建自己的连接。 你能展示你的被测路由的 mvp 实现的最小复制存储库,包括模块和依赖项吗?

最后我的错误在这一行:

导入:[AppModule, UserModule],

应用程序模块覆盖了我测试的猫鼬。连接内部建立的另一个连接。