Cannot figure out how to use MOCK_ENV
Hello everyone!
I honestly tried multiple approaches and read the docs several times. I searched other Hono repositories and saw a working example for Cloudflare Workers and they use MOCK_ENV exactly how I want to use it.
The only difference is the original source of environment variables. My suspicion is that MOCK_ENV cannot “win over” Bun‘s .env. I created a minimal reproduction with tests. Could someone please have a look?
Thank you all in advance!
@nawok
Try:
app.request('/env', MOCK_ENV)
I get "Type Environment has no properties in common with RequestInit" from TypeScript.
When I run bun test with these changes, I get the same result:
Expected: "mocked-env" Received: "development-env"
@nawok Sorry.
MOCK_ENV is used only in Cloudflare Workers/Pages. In the other runtime, you can not pass values with MOCK_ENV.
Hi @yusukebe! Is it a good feature request? Should I create a PR with your clarification in the docs?
We should have this function as is.
The MOCK_ENV will be passed as c.env, but it will be used only for Cloudflare Workers/Pages. This is because of a runtime limitation. Cloudflare Workers/Pages do not have a general environment variable that you can access globally, so you have to pass something like MOCK_ENV via c.env. On the other hand, other platforms can have environment variables that can be accessed globally, so c.env is unnecessary. The env function in hono/adapter does not use c.env values on a runtime other than Cloudflare Workers/Pages.
Should I create a PR with your clarification in the docs?
Yeah! We should add a description in the docs.