Comments (6)
However, Vitest does not currently support the
--env-file
flag, meaning developers who upgrade to Node.js 20 cannot take advantage of this new feature when running their tests.
If it's a Node CLI flag, it should be supported as usual. Try passing that flag to Node when running Vitest:
- tsx --env-file=.env run-e2e.ts
+ node --env-file=.env ./node_modules/vitest/vitest.mjs
If you wish to pass it only to test runners and not have it available in main thread (vitest.config.ts
, global setup), pass it in configuration:
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
poolOptions: {
threads: { execArgv: ["--env-file=.env"] },
// Or another pool:
forks: { execArgv: ["--env-file=.env"] },
},
},
});
from vitest.
However, Vitest does not currently support the
--env-file
flag, meaning developers who upgrade to Node.js 20 cannot take advantage of this new feature when running their tests.If it's a Node CLI flag, it should be supported as usual. Try passing that flag to Node when running Vitest:
- tsx --env-file=.env run-e2e.ts + node --env-file=.env ./node_modules/vitest/vitest.mjsIf you wish to pass it only to test runners and not have it available in main thread (
vitest.config.ts
, global setup), pass it in configuration:import { defineConfig } from "vitest/config"; export default defineConfig({ test: { poolOptions: { threads: { execArgv: ["--env-file=.env"] }, // Or another pool: forks: { execArgv: ["--env-file=.env"] }, }, }, });
Thanks! I just tested, but only the first solution using node --env-file=.env ./node_modules/vitest/vitest.mjs
worked. But I keep wondering why the Vitest CLI doesn't accept this Node.js flag if tsx
does.
from vitest.
tsx
forwards all options to node
because it's basically a Node wrapper. Vitest is a standalone application, so we accept only relevant options.
This lack of support necessitates the continued use of third-party packages for managing environment variables, which undermines the benefits provided by the new Node.js 20 feature.
This is not true because you can use loadEnv
from vite
which always comes with Vitest: https://vitejs.dev/guide/api-javascript.html#loadenv
Vitest also loads env automatically if you follow the convention: https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes
from vitest.
Vitest also loads env automatically if you follow the convention: https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes
In my case, I'm creating a Fastify app, so when I try to access import.meta.env
, I get the following error:
Property 'env' does not exist on type 'ImportMeta'.
Even if I declare a new global type like this:
declare global {
interface ImportMeta {
readonly env: z.infer<typeof envSchema>;
}
}
I still can't use import.meta.env
because it return undefined
from vitest.
I still can't use
import.meta.env
because it returnundefined
Vitest supports process.env
, it is equal to import.meta.env
in tests
In any way, you can use loadEnv
from vite
already without any additional packages: https://vitejs.dev/guide/api-javascript.html#loadenv
from vitest.
Vitest supports
process.env
, it is equal toimport.meta.env
in testsIn any way, you can use
loadEnv
fromvite
already without any additional packages: https://vitejs.dev/guide/api-javascript.html#loadenv
Great! So I'm gonna use loadEnv
from vite
. Thank you!
from vitest.
Related Issues (20)
- testNamePattern doesn't parse RegEx'es in the CLI, or documentation is unclear HOT 2
- get error "Could not parse CSS stylesheet" when CSS file contains nested rules HOT 2
- Issue with `vitest --run --changed` when used with lint-staged on Windows HOT 2
- Default reporter emits `prepare` longer than `Duration` when prepare takes longer than 1s HOT 3
- test.each does not print -0 and -NaN correctly
- `vi.fn()` performs an extra _await_ leading to mismatching behaviour when compared with normal function HOT 6
- restoreAllMocks doesn't restore mocks HOT 4
- API for timer mocks that are safely isolated between tests HOT 3
- There's no console.log output from 'beforeAll' and 'afterAll' when running tests via Webstorm / Aqua IDE HOT 1
- vitest fails to load typescript worker code in node (but does so correctly in browser mode) HOT 6
- Better output in UI mode for screen.debug() HOT 2
- ReferenceError on globals using Vitest HOT 2
- Additional type definitions are ignored
- Second (Custom Message) Argument Breaks expect().toBeVisible() HOT 2
- Missing link in Documentation mocking requests HOT 1
- vi.stubEnv documentation and typing differ from each other
- `MY_ENV=abc vitest` exposes `import.meta.env.MY_ENV === "abc"` HOT 2
- Support it() as well for bench HOT 2
- Vitest 1.6.0 upgrade breaks sandboxing the filesystem from tests HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vitest.