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

Importing @hono/zod-openapi in cloudflare worker vitest environment fails #436

Closed
JonasHiltl opened this issue Mar 28, 2024 · 2 comments · Fixed by #437 or #443
Closed

Importing @hono/zod-openapi in cloudflare worker vitest environment fails #436

JonasHiltl opened this issue Mar 28, 2024 · 2 comments · Fixed by #437 or #443

Comments

@JonasHiltl
Copy link

Cloudflare recently announced their new testing integration with vitest to test workers directly in a workers environment.

When trying it out myself I stumbled about this bug caused by @hono/zod-openapi because it can't import @hono/zod-validator. But if you look closely at the error message you can see that @hono/zod-openapi tries to import @hono/zod-validator from .../dist/@hono/zod-validator but that path is wrong, since dependencies are not located in dist.

[vpw:inf] Starting isolated runtimes for vitest.config.ts...
workerd/server/server.c++:2789: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=%2FUsers%2F...%2Foapi-bug%2Fnode_modules%2F%40hono%2Fzod-validator%2Fdist%2Fesm%2Findex.js&referrer=%2FUsers%2F...%2Foapi-bug%2Fnode_modules%2F%40hono%2Fzod-openapi%2Fdist%2Findex.mjs
 ❯ test/index.spec.ts (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "Users/.../oapi-bug/node_modules/@hono/zod-openapi/dist/@hono/zod-validator".
  imported from "Users/.../oapi-bug/node_modules/@hono/zod-openapi/dist/index.mjs"

Here is a minimal repo to reproduce the bug.
The bug is caused by just importing from @hono/zod-openapi, so this is enough

import { OpenAPIHono } from "@hono/zod-openapi";
import { test } from "vitest";

test("test", () => {
  const app = new OpenAPIHono();
})

This smells like an esm error to me, but I'm not sure why it searches for the dependency in dist 🤷‍♂️

@JonasHiltl JonasHiltl changed the title [@hono/zod-openapi] Test in cloudflare worker vitest environment fails Importing @hono/zod-openapi in cloudflare worker vitest environment fails Mar 28, 2024
@yusukebe
Copy link
Member

Hi @JonasHiltl

Thanks for raising the issue and creating the repro.

I've released the new version of @hono/zod-openapi. This will fix the "Hono-side" problem. But, unfortunately, Zod to OpenAPI, which is used in @hono/zod-open API, is not supporting ESM now. So the following error will thrown.

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
TypeError: Cannot use require() to import an ES Module.
 ❯ Users/yusuke/work/h/oapi-bug/node_modules/openapi3-ts/dist/oas30-CAmzubQd.js:1:22
 ❯ Users/yusuke/work/h/oapi-bug/node_modules/openapi3-ts/dist/oas30.js?mf_vitest_no_cjs_esm_shim:1:27
 ❯ Users/yusuke/work/h/oapi-bug/node_modules/@asteasolutions/zod-to-openapi/dist/index.js?mf_vitest_no_cjs_esm_shim:35

@yusukebe
Copy link
Member

yusukebe commented Apr 7, 2024

@JonasHiltl

This issue has been fixed completely with the new version @hono/zod-openapi@0.10.0. Try it!

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