I am doing some testing and I just want to test my endpoint. What goes in and what does out that's it.
I have a test database that I want to use when the test run.
in "BeforeAll" I connect to the test database and in my beforeach I make a post of a user. It work it's inserted in the test database
The problem is as soon as I try to make a request in a "It" statement , the database used is app one not the test database :/
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))
});
});
I am doing it wrong ? Thank you guys !
Finally my mistake was in this line :
imports: [AppModule, UserModule],
App module override my test's mongoose. connect with another connection established inside.