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');
});
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:
Additional context
server/routes/something.ts
test/routes/something.spec.ts
Logs
No response