vitest icon indicating copy to clipboard operation
vitest copied to clipboard

setupFiles does not import npm module correctly in mono repo project setup

Open gb-jos opened this issue 3 years ago • 3 comments

Describe the bug

I have a mono repo project, where I am trying to use vitest to test one of the monorepo packages. I need some polyfills in the environment and I am trying to loading this using setupFiles and mentioning the npm package name there. As far as I have read the configuration documentation, this should work. But unfortunately it fails.

Seemingly the problem is with the path resolution, the path seems to have been resolved correctly, but then root is prefixed on this absolute path. Find attached screenshot from shared reproduction stackblitz Screenshot 2022-09-12 at 4 42 08 PM

Reproduction

stackblitz minimal repro

  1. Run npm run test in packages/package.
  2. Notice the error message regarding failing to import the reflect-metadata module

System Info

latest vite and vitest

Used Package Manager

npm

Validations

gb-jos avatar Sep 12 '22 14:09 gb-jos

cc @sheremet-va

gb-jos avatar Sep 12 '22 15:09 gb-jos

Seems the problem happens here.

https://github.com/vitest-dev/vitest/blob/95da4d66933db46da368d5d66f422afd26fba4c1/packages/vite-node/src/utils.ts#L77-L84

Isn't it weird resolve(root, id.slice(1) when id is an abolute path @sheremet-va

sun0day avatar Sep 17 '22 14:09 sun0day

@sun0day I can confirm that in my case the problem happens here.

Screenshot 2022-09-19 at 9 43 58 AM

its a monorepo, the tests are running inside lambda-logger package, and the reflect-metadata is hoisted to root node_modules folder. I guess this will only happen in monorepo setup, as otherwise id.startsWith(root) will be true.

gb-jos avatar Sep 19 '22 07:09 gb-jos

I think this issue may solve by setting

{
test: {
  // `vitest` use the outside dir as root, different from `vite`
  root: resolve(__dirname, "../../")
}
}

But now there are two problems:

  1. test.root is not effective. See #2050.
  2. vite-node still use vite.root. See below: https://github.com/vitest-dev/vitest/blob/5b8d6dbe75222aeeee897a3129222818c010bdd8/packages/vite-node/src/server.ts#L121

For fix problem two, could solve by use this.options.root.

But I'm not sure if this would cause other problems.

PatrickChen928 avatar Sep 27 '22 03:09 PatrickChen928

@ChpShy I would consider that as a hack rather than a solution, as even though it would solve the resolution, it would also cause vitest to include all tests from other projects in the monorepo.

A better hack (which I am using right now) is to create a local file inside the project, that internally imports the dependency, and use that in the setupFiles instead of the dependency directly.

gb-jos avatar Sep 27 '22 09:09 gb-jos

Also, this behavior works in monorepo as expected with older version 0.23.4. This issue prevents me from updating to the latest version.

mc-petry avatar Dec 01 '22 18:12 mc-petry

This should actually be fixed I think. Can you confirm it doesn’t work with the latest version?

sheremet-va avatar Dec 01 '22 18:12 sheremet-va

It's not fixed yet (or not fully fixed). I have monorepo with packages:

  • @repo/app
  • @repo/testing

@repo/testing contains setup file:

import { vi } from 'vitest'

console.log('init')
vi.mock('framer-motion', async () => {
  console.log('mock')
  const framer = await vi.importActual<object>('framer-motion')

  return {
    ...framer,
    AnimatePresence: ({ children }: { children: React.ReactNode }) => children,
  }
})

In package @repo/app i use setup file from @repo/testing.

Actual console.log for v0.23.4:

stdout | unknown test
init

stdout | unknown test
mock

Actual console.log for v0.25.3:

stdout | unknown test
init

So in fact for 0.25.3 file is imported but vi.mock not working.

mc-petry avatar Dec 02 '22 11:12 mc-petry

So in fact for 0.25.3 file is imported but vi.mock not working.

So how is this related to this issue? This issue is specifically about importing setupFile, but in your example mock is not called. If you have a different problem, please create a separate issue, if it doesn't already exist.

I ran reproduction from this issue with the latest version and it's working fine, so I'm closing this issue.

sheremet-va avatar Dec 02 '22 12:12 sheremet-va