setupFiles in workspace configuration #7367
-
Hi everyone, I have a monorepo setup, and I’m using Vitest to run tests globally across all my packages. My goal is to create a workflow where I can run tests for each package individually while still sharing the same global configuration and environment setup. To achieve this, I started exploring workspaces, but I’ve run into an issue. I load environment variables using a file specified in the setupFiles parameter of my Vitest configuration. However, when I add workspace-specific configuration, it seems like the setupFiles file is being ignored. Additionally, my packages are nested and organized into subfolders with varying depths. Since I have many packages, defining each one individually isn’t practical. I’m not sure how to properly configure workspaces to account for this structure. I’ve searched the documentation but haven’t been able to find a solution. Has anyone faced a similar issue, or can you suggest how to handle this setup? Thanks in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Assuming you can at least obtain a list of monorepo package directories which you want to run the tests, I think you can configure workspaces programatically like: // vite.config.ts (just only one in monorepo root)
import { defineConfig } from "vitest/config";
const packageDirs = someGlobLib(["packages/*", "packages/and/more/*"])
export default defineConfig({
test: {
setupFiles: ["..."], // will be shared via `extends: true`
workspace: packageDirs.map(dir => ({
extends: true,
test: {
name: dir,
// setupFiles: ["..."] // or put it individually
}
}))
}
}) |
Beta Was this translation helpful? Give feedback.
Thanks for the reply! Eventually after searching other discussions I found a different way to load the environment variables other than having setupFiles script. And regarding workspaces, I've listed every subfolder individiually into config, but I'm sure your solution would work as well.