Reputation: 1
I've written some integration test using Jest, Supertest, Moongose. I run all test isolated (test.only) and they work but sometimes don't. Often this happens when I run the entire test suite. I give you an example: This test creates a new registry in a MongoDB collection and then other test use this new registry to perform another operations:
beforeAll(async () => {
await mongoose.connect(config.mongoose.url, config.mongoose.options);
});
afterAll(async () => {
await mongoose.disconnect();
await new Promise(resolve => setTimeout(() => resolve(), 500));
});
let credentials = [];
let fechaHora = [];
// generate a new Id registry
// generate credentials
// generate date and hour
beforeEach(async () => {
rooms.insertRoomsToFile(rooms.getNewIdRoom() + '|');
_idRoom = rooms.getIdRoom();
credentials = await rooms.generateCredentialsBE(_idOrganization, basicToken);
fechaHora = rooms.generateRoomDateAndHour();
});
test(`endpoint ${BASE_URL}${registerMeetingRoute} : 200 OK (Happy Path)`, async () => {
generatedIdRoom = _idRoom;
const data = {
idOrganization: 1,
idRoom: generatedIdRoom,
date: fechaHora[0],
hour: fechaHora[1],
attendes: [
{
email: "[email protected]",
id: 1,
firstName: "John",
lastName: "Doe",
userAttende: "10000000-0000-0000-0000-000000000000",
rol: 1,
telephone: "5555555555"
},
{
email: "[email protected]",
id: 2,
firstName: "Tom",
lastName: "Taylor",
userAttende: "20000000-0000-0000-0000-000000000000",
rol: 2,
telephone: "5555555556"
}
]
};
const encryptedData = await rooms.encrypt(data);
idAccess = encryptedData.idAccess;
await request(app)
.post(`${BASE_URL}${registerMeetingRoute}`)
.set('content-type', 'application/json')
.set('authorization', 'Bearer ' + credentials[2])
.set('x-accessId', idAccess)
.send(encryptedData)
.expect(200);
rooms.saveLog(JSON.stringify(encryptedData), `endpoint ${BASE_URL}${registerMeetingRoute} : 200 OK (Happy Path)`);
});
This works fine, the problem is sometimes don't. I've tried many answers here and read blogs about this topic but I can't solve it. I tried:
Thanks in advance :)
Upvotes: 0
Views: 1736
Reputation: 116
Jest will execute all tests within a single file sequentially.
However, I've also encountered issues with the complete file run vs. running a single test, in case a plug-in a router with WebHashHistory
into the wrapper mount. The problem does not occur with MemoryHistory
:
const router = createRouter({
history: createMemoryHistory(),
routes: myRoutes,
})
Vue 3.2, Vue-router 4.0, vue-test-utils v2.0, options API, SFC, Jest v27.0
Upvotes: 0
Reputation: 6288
Jest will execute different test files in parallel and in a different order from run to run.
You can change this behaviour and write your own custom test sequencer.
Upvotes: 1