Skip to content

vi.mock in Browser mode with Nx (monorepo) hangs over #6545

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

You must be logged in to vote

I checked your reproduction and it looks like there's something wrong with server.fs.allow. Adding this helps getting __vitest_msw__ work:

// vite.config.ts
  server: {
    fs: {
      allow: [path.join(__dirname, "../..")]
    }
  },

Another issue (partly on your code) is that you have to use vi.hoisted to use variables inside vi.mock

// [not ok]
// const mockedIsOk = vi.fn().mockReturnValue(true);
// [ok]
const mockedIsOk = vi.hoisted(() => vi.fn().mockReturnValue(true))

vi.mock('./isOk', async (importActual) => {
  // ...
  return {
    isOk: mockedIsOk,
  };
});

Without browser mode, the wrong code shows ReferenceError: Cannot access 'mockedIsOk' before initialization, but it looks l…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@hi-ogawa
Comment options

@dhythm
Comment options

Answer selected by dhythm
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