Skip to content

How to do mocks when testing a nodejs custom environment to test a fastify server? #6365

Answered by hi-ogawa
akhilmhdh asked this question in Q&A
Discussion options

You must be logged in to vote

I think environment loading happens before any module graph setup (namely VitestExecutor), so probably that's why vi.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 in setupFiles?

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 environ…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@akhilmhdh
Comment options

@hi-ogawa
Comment options

@akhilmhdh
Comment options

@hi-ogawa
Comment options

@akhilmhdh
Comment options

Answer selected by akhilmhdh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants