Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
6 tasks done
jiby-aurum opened this issue Sep 12, 2022 · 9 comments
Closed
6 tasks done

Comments

@jiby-aurum
Copy link

jiby-aurum commented Sep 12, 2022

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

@jiby-aurum
Copy link
Author

cc @sheremet-va

@sun0day
Copy link
Contributor

sun0day commented Sep 17, 2022

Seems the problem happens here.

export function toFilePath(id: string, root: string): string {
let absolute = id.startsWith('/@fs/')
? id.slice(4)
: id.startsWith(root)
? id
: id.startsWith('/')
? resolve(root, id.slice(1))
: id

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

@jiby-aurum
Copy link
Author

@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.

@PatrickChen928
Copy link
Contributor

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 Root option ignored if vite config also has root option #2050.
  2. vite-node still use vite.root. See below:
    const filePath = toFilePath(id, this.server.config.root)

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

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

@jiby-aurum
Copy link
Author

@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.

@mc-petry
Copy link

mc-petry commented Dec 1, 2022

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

@sheremet-va
Copy link
Member

sheremet-va commented Dec 1, 2022

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

@mc-petry
Copy link

mc-petry commented Dec 2, 2022

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.

@sheremet-va
Copy link
Member

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.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants