linux-china / vitest-jetbrains-plugin

Vitest JetBrains plugin
https://plugins.jetbrains.com/plugin/19220-vitest-runner
Apache License 2.0
52 stars 6 forks source link

read `vitest.config` for monorepos #10

Closed meoyawn closed 2 years ago

meoyawn commented 2 years ago

Hi, thank you so much for this

I am reading .env.test files in my tests like this in vitest.config.ts:

import { defineConfig, loadEnv } from "vite"

// noinspection JSUnusedGlobalSymbols
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "")
  Object.assign(process.env, env)
  return {}
})

but my env vars are not loaded when running vitest with this plugin

Any help appreciated

linux-china commented 2 years ago

@meoyawn what is your command line to run vitest? Now the plugin just invokes vitest run -t first demo.test.ts command line.

meoyawn commented 2 years ago

@linux-china thanks for responding

I run tests with vitest run src/, it loads vitest.config.ts (with loadEnv) then runs everything

meoyawn commented 2 years ago

@linux-china oh, I just realized the problem was my monorepo. I have several subpackages in a monorepo: front, back, shared

monorepo file structure: package.json back/package.json back/vitest.config.ts (not loaded) back/src/__tests__/demo.test.ts

only one of my subpackages needs to load .env.test, so I just specified it in root vitest.config.ts:

import { defineConfig, loadEnv } from "vite"
import { join } from "path"

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, join(process.cwd(), "back"), "")
  Object.assign(process.env, env)
  return {}
})

alright, my issue is solved. Thanks again