refactor: extracted fetch mocking into reusable hookFetch utility
- Extracted fetch mocking logic into reusable `hookFetch()` utility function with middleware-style handler pattern. - Replaced manual `globalThis.fetch` assignment and restoration across 10 test files with `hookFetch()` calls using `using` statement for automatic cleanup. - Implemented Disposable pattern with Symbol.dispose for fetch hook resource management, eliminating try-finally blocks. - Exported `hookFetch` from utils public API to enable consistent fetch mocking across packages.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
---
|
||||
description: Use hookFetch instead of assigning globalThis.fetch directly in tests
|
||||
condition: "globalThis\\.fetch\\s*="
|
||||
scope: "tool:edit(**/*.test.{ts,tsx,js,jsx}), tool:write(**/*.test.{ts,tsx,js,jsx})"
|
||||
---
|
||||
|
||||
**Do not assign `globalThis.fetch = ...` directly in tests.**
|
||||
|
||||
## Why it's wrong
|
||||
|
||||
- It bypasses the project's standard fetch mocking helper
|
||||
- It is easier to forget restoration and leak state across tests
|
||||
- It makes test mocking inconsistent across the codebase
|
||||
|
||||
## What to use instead
|
||||
|
||||
Use `hookFetch` from `@oh-my-pi/pi-utils`:
|
||||
|
||||
```ts
|
||||
import { hookFetch } from "@oh-my-pi/pi-utils";
|
||||
|
||||
using _hook = hookFetch((input, init, next) => {
|
||||
// return a mocked Response, or delegate with next(input, init)
|
||||
});
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
```ts
|
||||
// WRONG
|
||||
globalThis.fetch = async () => new Response("ok");
|
||||
|
||||
// RIGHT
|
||||
using _hook = hookFetch(() => new Response("ok"));
|
||||
```
|
||||
|
||||
If you need to intercept fetch in tests, use `hookFetch`.
|
||||
Reference in New Issue
Block a user