fix(tests): address Task 4 code review feedback
- Deprecate mockPayloadModule with throw guard (hoisting incompatible) - Replace `as never` with stubPayloadConfig in payload-articles test (consistency) - Tighten pages contract to use toHaveLength (exact assertions) - Header contract: define CONTRACT_HEADER_SEED, assert item count + order Reviewer: superpowers:code-reviewer (Task 4 of Plan 7). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,27 @@
|
||||
import { vi } from "vitest";
|
||||
import type { Payload } from "payload";
|
||||
|
||||
// Helper to mock the `payload` package with a custom getPayload impl.
|
||||
// Call inside a test file BEFORE importing the SUT.
|
||||
/**
|
||||
* @deprecated DO NOT USE — hoisting incompatible.
|
||||
*
|
||||
* Vitest statically hoists `vi.mock()` calls to the top of the file.
|
||||
* This helper wraps `vi.mock()` inside a regular function body, so the
|
||||
* hoist never fires — the mock is installed too late and the real
|
||||
* `payload` module loads before being intercepted.
|
||||
*
|
||||
* Instead, write at the TOP LEVEL of your test file:
|
||||
*
|
||||
* vi.mock("payload", () => ({ getPayload: vi.fn() }));
|
||||
*
|
||||
* // then in your test:
|
||||
* const { getPayload } = await import("payload");
|
||||
* (getPayload as ReturnType<typeof vi.fn>).mockResolvedValue(yourStub);
|
||||
*
|
||||
* This export is preserved only so existing imports don't break and
|
||||
* agents searching for it find this warning.
|
||||
*/
|
||||
export function mockPayloadModule(impl: Partial<Payload>): void {
|
||||
vi.mock("payload", () => ({
|
||||
getPayload: vi.fn().mockResolvedValue(impl),
|
||||
}));
|
||||
void impl;
|
||||
throw new Error(
|
||||
"mockPayloadModule is deprecated and broken; see JSDoc. Call vi.mock('payload', ...) at the top of your test file instead.",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user