nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
287 stars 74 forks source link

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

Closed pmwmedia closed 2 months ago

pmwmedia commented 2 months ago

Environment

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 commented 2 months ago

Yes, the vitest environment is a client environment.

We can track nitro environment via https://github.com/nuxt/test-utils/issues/531.