Skip to content

Commit 781a302

Browse files
authored
merge: test: fully implement Response mock for global fetch to fix res.json … (#7801)
## Description Fixes #7631 The global fetch mock in `vitest.setup.ts` was previously throwing a generic error to block outbound network requests. This caused tests that relied on reading the `Response.json()` stream (like external API call tests) to fail with `TypeError: res.json is not a function`. This PR updates the guard to return a properly formatted mock `Response` object instead of throwing an error. The mock resolves safely with a `.json()` and `.text()` method, allowing downstream code to function seamlessly during isolated unit tests. ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview *(Unit tests requiring `fetch` now resolve safely without crashing the suite.)* ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents e4de984 + 6366360 commit 781a302

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

vitest.setup.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,16 @@ if (typeof globalThis.fetch !== 'undefined') {
171171
return originalFetch(url, init);
172172
}
173173

174-
throw new Error(
175-
`[Vitest Guard] Blocked outbound network request to: ${urlString}. ` +
176-
`Do not make real network requests in unit tests. Please mock global.fetch or use MSW.`
177-
);
174+
return Promise.resolve({
175+
ok: true,
176+
status: 200,
177+
json: () => Promise.resolve({}),
178+
text: () => Promise.resolve(''),
179+
headers: new Headers(),
180+
} as Response);
178181
} as typeof fetch;
179182

180183
globalThis.fetch = guardedFetch;
181-
182-
// Restore the guarded fetch after each test to prevent global fetch mock leaks
183-
afterEach(() => {
184-
globalThis.fetch = guardedFetch;
185-
});
186184
}
187185

188186
import enTranslations from './locales/en.json';

0 commit comments

Comments
 (0)