examples icon indicating copy to clipboard operation
examples copied to clipboard

Cannot figure out how to use MOCK_ENV

Open nawok opened this issue 1 year ago • 5 comments

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 avatar Sep 24 '24 18:09 nawok

@nawok

Try:

app.request('/env', MOCK_ENV)

yusukebe avatar Sep 25 '24 04:09 yusukebe

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 avatar Sep 25 '24 06:09 nawok

@nawok Sorry.

MOCK_ENV is used only in Cloudflare Workers/Pages. In the other runtime, you can not pass values with MOCK_ENV.

yusukebe avatar Sep 25 '24 07:09 yusukebe

Hi @yusukebe! Is it a good feature request? Should I create a PR with your clarification in the docs?

nawok avatar Sep 25 '24 07:09 nawok

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.

yusukebe avatar Sep 25 '24 07:09 yusukebe