t3-env issues with Vitest test cases
It looks like client environments are not being read. No issues with the server envs.
39 Replies
It is certainly not empty in my .env
Need more than that screenshot to help you…
my
env.mjs
the test case
trace
api.ts
and the env itself
I'm on next 13.4.3 and @t3-oss/env-nextjs 0.3.1
I am unable to share the repo sadly as it is a confidential work repo
Also running pnpm build works fine too weirdly enoughAlso in dev, I have access to all env values
Seems like the test is the only issue
maybe my config is wrong 🤔
With the environment set to node, it doesnt see any envs
whereas with jsdom only the NEXT_PUBLIC_API_URL is invalid
You have to turn of env validation if your not Gona give the env vars
Create env takes a option for that
but the env vars are in my .env
here
so im not sure why
pnpm test
doesnt see that skipValidation: !!process.env.SKIP_ENV_VALIDATION,
Oh true. But I guess you won't always have them when you run your tests
You could make it somthing like
skipValidation: !!process.env.SKIP_ENV_VALIDATION || !!import.meta.vitetest
whats || !!import.meta.vitetest
Skips validation when it's run via vitest
Or atleast I'm hoping
im not sure if thats the right approach
cause
the env is still empty
{
NEXTAUTH_SECRET: undefined,
NEXTAUTH_URL: undefined,
NEXT_PUBLIC_API_URL: undefined
}
this is when i log the full env in some test
Ahhh yeah it won't be then
Hope Julius has any ideas
Have you got a minimal repro?
What do you mean?
A repo that contains just the code needed to reproduce the issue
I dont have the time to set that up right now, im at work haha
does it have to do with the VITE_ prefix maybe
looks like it....
damn thats inconvenient
Fairs
Hmmm is there anything in Thier docs about env vars?
not really i believe
might have to add dotenv package
Oh yeah 🤦♂️ . Your using next. Should have seen that sooner
there's gotta be some workaround for this no?
@marminge have you come across this issue whilst building the
t3-complete
repo that also uses Vitest?Don't think complete does unit testing
Can you call dotenv from your vite config? That might work
It does with Vitest and Playwright
not sure how dotenv works, gotta figure that out
Those are e2e tests not unit test. I asume it brings up the next app. Hence no env errors
require("dotenv").config()
rest is automatic?
no extra config files
damn that actually seems to work
Ya
without needing VITE_
That's all next does under the hood
only gotta fix these now..
Quick fix => ignore
theres no jsdoc @type for that?
Dunno. Don't need it tho
welp it works for now. thanks for your effort.
eventual fix was adding this to vitest.config.ts
@Finn ( CLOwn ) thanks for your time and effort
Noice nw
I couldn't get this to work.
Instead I added the following line to defineConfig:
Full code here:
And also specify the mode when running vitest:
@stuartrobinson what file is that config in? Is that just your vitest.config.ts?
That’s right