mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 05:30:41 +01:00
Jest is CJS-rooted, needs transform configuration for ESM and TypeScript, and
every shared CI template has to accommodate its dialect. vitest is ESM/TS
native and jest-compatible at the API level.
`globals: true` keeps describe/it/expect/beforeEach/vi available without
imports, so the diff is the jest.* API names and nothing else. Only TYPES had
to be imported, because a type cannot come from a global.
next/jest supplied the path alias, the next/server mock and the jsdom
environment implicitly; vitest.config.ts states them, so they are visible
rather than inherited. The transformIgnorePatterns override for ESM-only
packages (ai-kit) is DELETED rather than translated — jest ran CJS and died on
`Unexpected token export`, which is the entire reason that workaround existed.
vitest loads ESM natively.
Three things only running the suite could have found:
* __mocks__/next/server.ts was outside the first rewrite pass, so every test
importing it died with `jest is not defined`.
* vitest builds a mocked constructor with Reflect.construct, and an ARROW
function is not constructible — `vi.fn().mockImplementation(() => ({...}))`
threw "is not a constructor" the moment the code did `new SESClient()`.
jest tolerated it. Two mocks needed function expressions.
* removing @types/jest took the global describe/it/expect declarations with
it; tsconfig now carries "types": ["vitest/globals"].
Verified locally before pushing: 272 passed, 2 skipped, 23 files.
Claude-Session: https://claude.ai/code/session_01UvjGNAS9CMfEGNW26tUR4P
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
124 lines
2.9 KiB
TypeScript
124 lines
2.9 KiB
TypeScript
import { vi } from 'vitest';
|
|
/**
|
|
* Mock for next/server in Jest tests
|
|
*
|
|
* This allows tests to import modules that use NextResponse/NextRequest
|
|
* without loading the actual Next.js server runtime.
|
|
*/
|
|
|
|
// Use global Request/Response if available (jsdom), otherwise create simple mocks
|
|
const BaseRequest =
|
|
globalThis.Request ||
|
|
class MockRequest {
|
|
url: string;
|
|
method: string;
|
|
headers: Map<string, string>;
|
|
body: unknown;
|
|
|
|
constructor(input: string | URL, init?: RequestInit) {
|
|
this.url = input.toString();
|
|
this.method = init?.method || 'GET';
|
|
this.headers = new Map(Object.entries(init?.headers || {}));
|
|
this.body = init?.body;
|
|
}
|
|
|
|
json() {
|
|
return Promise.resolve(typeof this.body === 'string' ? JSON.parse(this.body) : this.body);
|
|
}
|
|
};
|
|
|
|
const BaseResponse =
|
|
globalThis.Response ||
|
|
class MockResponse {
|
|
body: string | null;
|
|
status: number;
|
|
headers: Map<string, string>;
|
|
|
|
constructor(body?: string | null, init?: ResponseInit) {
|
|
this.body = body || null;
|
|
this.status = init?.status || 200;
|
|
this.headers = new Map(Object.entries(init?.headers || {}));
|
|
}
|
|
|
|
json() {
|
|
return Promise.resolve(this.body ? JSON.parse(this.body) : null);
|
|
}
|
|
};
|
|
|
|
export class NextRequest extends BaseRequest {
|
|
nextUrl: URL;
|
|
|
|
constructor(input: string | URL, init?: RequestInit) {
|
|
super(input, init);
|
|
this.nextUrl = new URL(typeof input === 'string' ? input : input.toString());
|
|
}
|
|
|
|
get cookies() {
|
|
return {
|
|
get: vi.fn(),
|
|
getAll: vi.fn(() => []),
|
|
set: vi.fn(),
|
|
delete: vi.fn(),
|
|
has: vi.fn(() => false),
|
|
};
|
|
}
|
|
|
|
get geo() {
|
|
return {};
|
|
}
|
|
|
|
get ip() {
|
|
return '127.0.0.1';
|
|
}
|
|
}
|
|
|
|
export class NextResponse extends BaseResponse {
|
|
static json(body: unknown, init?: ResponseInit) {
|
|
const response = new NextResponse(JSON.stringify(body), {
|
|
...init,
|
|
headers: {
|
|
...init?.headers,
|
|
'content-type': 'application/json',
|
|
},
|
|
});
|
|
return response;
|
|
}
|
|
|
|
static redirect(url: string | URL, status?: number) {
|
|
return new NextResponse(null, {
|
|
status: status || 307,
|
|
headers: { Location: url.toString() },
|
|
});
|
|
}
|
|
|
|
static rewrite(destination: string | URL) {
|
|
return new NextResponse(null, {
|
|
headers: { 'x-middleware-rewrite': destination.toString() },
|
|
});
|
|
}
|
|
|
|
static next() {
|
|
return new NextResponse(null);
|
|
}
|
|
|
|
get cookies() {
|
|
return {
|
|
get: vi.fn(),
|
|
getAll: vi.fn(() => []),
|
|
set: vi.fn(),
|
|
delete: vi.fn(),
|
|
};
|
|
}
|
|
}
|
|
|
|
export const userAgent = vi.fn(() => ({
|
|
isBot: false,
|
|
browser: { name: 'Chrome', version: '100' },
|
|
device: { type: undefined, vendor: undefined, model: undefined },
|
|
engine: { name: 'Blink', version: '100' },
|
|
os: { name: 'Mac OS', version: '12' },
|
|
cpu: { architecture: undefined },
|
|
}));
|
|
|
|
export const userAgentFromString = vi.fn(() => userAgent());
|