Skip to content
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

Cannot figure out how to use MOCK_ENV #213

Open
nawok opened this issue Sep 24, 2024 · 5 comments
Open

Cannot figure out how to use MOCK_ENV #213

nawok opened this issue Sep 24, 2024 · 5 comments

Comments

@nawok
Copy link

nawok commented Sep 24, 2024

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 added a commit to nawok/hono-env-testing that referenced this issue Sep 24, 2024
nawok added a commit to nawok/hono-env-testing that referenced this issue Sep 24, 2024
@yusukebe
Copy link
Member

@nawok

Try:

app.request('/env', MOCK_ENV)

@nawok
Copy link
Author

nawok commented Sep 25, 2024

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"

@yusukebe
Copy link
Member

yusukebe commented Sep 25, 2024

@nawok Sorry.

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

@nawok
Copy link
Author

nawok commented Sep 25, 2024

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

@yusukebe
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants