-
Hey everyone, We are using vitest to test our fastify nodejs server. How we do is that is using the vitest custom environment option. So our custom environment file would looks like this // load environment
export default {
name: "env",
transformMode: "ssr",
async setup() {
// connect with db
try {
// do migration
// do seeding
const server = await main({ db, smtp, logger, queue, keyStore });
// @ts-expect-error type
globalThis.testServer = server;
} catch (error) {
console.log("[TEST] Error setting up environment", error);
await db.destroy();
throw error;
}
console.log("ended environment");
// custom setup
return {
async teardown() {
await globalThis.testServer.close();
await db.destroy();
}
};
}
}; A simple spec file would look like this
This seems to be working really well for us. Until we wanted some mocking to do on testing on endpoints. Like axios as an example. For some reason none of mocks are working. Like ones setup in Any insights would be helpful! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
I think environment loading happens before any module graph setup (namely Probably you had some reasons to choose custom environment approach, but just in case, sharing another way to achieve similar server setup using higher level API (so it should compose better with other APIs like beforeAll(async () => {
// connect with db
try {
// do migration
// do seeding
const server = await main({ db, smtp, logger, queue, keyStore });
globalThis.testServer = server;
} catch (error) {
console.log("[TEST] Error setting up environment", error);
await db.destroy();
throw error;
}
console.log("ended environment");
// custom setup
return async () => {
await globalThis.testServer.close();
await db.destroy();
}
}) |
Beta Was this translation helpful? Give feedback.
I think environment loading happens before any module graph setup (namely
VitestExecutor
), so probably that's whyvi.mock
wouldn't work.Probably you had some reasons to choose custom environment approach, but just in case, sharing another way to achieve similar server setup using higher level API (so it should compose better with other APIs like
vi.mock
).Have you considered something like this using
beforeAll
insetupFiles
?