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

vitest does not resolve nitro (h3) auto-imports #827

Closed
pmwmedia opened this issue Apr 24, 2024 · 1 comment
Closed

vitest does not resolve nitro (h3) auto-imports #827

pmwmedia opened this issue Apr 24, 2024 · 1 comment

Comments

@pmwmedia
Copy link

Environment

  • Operating System: Linux
  • Node Version: v18.18.0
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-xdspn8

Describe the bug

It is impossible to test server API and server route implementations directly, because vitest does not auto import nitro (h3) functions like e.g. defineEventHandler().

When executing the test in the linked minimal reproduction, I receive this error:

FAIL  test/server/routes/something.spec.ts [ test/server/routes/something.spec.ts ]
ReferenceError: defineEventHandler is not defined
 ❯ eval server/routes/something.ts:1:217
 ❯ eval test/server/routes/something.spec.ts:2:31

Additional context

server/routes/something.ts

export default defineEventHandler(() => 'OK');

test/routes/something.spec.ts

import { it } from 'vitest';
import handler from '../../../server/routes/something';

it('should return OK', () => {
  const response = handler({} as any);
  expect(response).toBe('OK');
});

Logs

No response

@danielroe
Copy link
Member

Yes, the vitest environment is a client environment.

We can track nitro environment via #531.

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants