-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
Environment variables declared in vitest.config.ts undefined when using the extension #326
Comments
This might be a same issue as this: @sambarfoot Could you check if it works from |
I did some testing this morning and it didn't work from the root of the project but it does from within the packages. You can test using the example from vitest#5016 - https://stackblitz.com/edit/github-2srz39?file=packages%2Flib1%2Fvite.config.ts ~/projects/github-2srz39/packages/lib1
❯ npx vitest --run
RUN v1.4.0 /home/projects/github-2srz39/packages/lib1
✓ test/double.test.ts (3)
✓ double
✓ env
✓ define
Test Files 1 passed (1)
Tests 3 passed (3)
Start at 07:15:55
Duration 1.40s (transform 41ms, setup 0ms, collect 25ms, tests 2ms, environment 0ms, prepare 163ms) ~/projects/github-2srz39
❯ npx vitest --run
RUN v1.4.0 /home/projects/github-2srz39
✓ |lib2| test/square.test.ts (2)
❯ |lib1| test/double.test.ts (3)
✓ double
× env
✓ define
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL |lib1| test/double.test.ts > env
AssertionError: expected undefined to be 'hello' // Object.is equality
- Expected:
"hello"
+ Received:
undefined
❯ eval test/double.test.ts:9:35
7| test('env', () => {
8| // @ts-ignore
9| expect(process.env['SOME_ENV']).toBe('hello');
| ^
10| });
11|
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Test Files 1 failed | 1 passed (2)
Tests 1 failed | 4 passed (5)
Start at 07:21:30
Duration 1.32s (transform 28ms, setup 0ms, collect 29ms, tests 7ms, environment 0ms, prepare 311ms) |
Thanks for checking. Yeah, that's the same behavior as vitest-dev/vitest#5016. For now, the workaround I can suggest is to use Or if you don't need isolate environment variables for each project, then you could directly assign import { defineProject } from 'vitest/config'
import tsconfigPaths from 'vite-tsconfig-paths';
process.env.DATABASE_URL = "http://localhost:3000"
export default defineProject({
plugins: [tsconfigPaths()],
}); |
@hi-ogawa Setting environment variables via
/// <reference types="vitest" />
import { resolve } from "path";
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
alias: {
"@": resolve(__dirname, "./src"),
},
watch: false,
environment: "jsdom",
reporters: ["verbose"],
setupFiles: ["./vitest-setup.mts"],
},
});
import "@testing-library/jest-dom/vitest";
import { loadEnvConfig } from "@next/env";
// Because I use nextjs, this api can be used to load ".env" file
loadEnvConfig(process.cwd()); When run But in the Did I miss something? System Info
|
@1111mp Please provide a reproduction if you're having a problem with
If using From a quick look, it looks like you might need to pass |
Ok, you can reproduce the issue via this repository: https://github.com/1111mp/next-temp-app
I am using the preview version of Only preview version works fine.
This has nothing to do with the /// <reference types="vitest" />
import { resolve } from "path";
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig(async () => {
const { loadEnvConfig } = (await import("@next/env")).default;
const { combinedEnv } = loadEnvConfig(process.cwd());
console.log(combinedEnv);
return {
plugins: [react()],
test: {
alias: {
"@": resolve(__dirname, "./src"),
},
env: {
...(combinedEnv as Record<string, string>),
},
watch: false,
environment: "jsdom",
reporters: ["verbose"],
setupFiles: ["./vitest-setup.mts"],
},
};
}); will print:
It still doesn't work for the /// <reference types="vitest" />
import { resolve } from "path";
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig(() => {
return {
plugins: [react()],
test: {
alias: {
"@": resolve(__dirname, "./src"),
},
env: {
TEST: "true",
VITEST: "true",
NODE_ENV: "test",
DATABASE_URL: "file:./db.sqlite",
APP_AUTH_KEY: "app_auth_key",
APP_RANDOM_PASSWORD: "################",
REDIS_HOST: "127.0.0.1",
REDIS_PORT: "6379",
REDIS_DB: "0",
REDIS_USERNAME: "root",
REDIS_PWD: "root",
SMTP_HOST: "smtp.office365.com",
SMTP_PORT: "587",
EMAIL_FROM: '"The1111mp" <[email protected]>',
SMTP_USER: "[email protected]",
SMTP_PASSWORD: '###########'
},
watch: false,
environment: "jsdom",
reporters: ["verbose"],
setupFiles: ["./vitest-setup.mts"],
},
};
}); |
Ah, I wasn't clear but what I meant was next |
Describe the bug
If env variables are defined in the vitest config files, they are undefined when using the extension
Reproduction
vitest.workspace.ts
vitest.config.mts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: