Skip to content

test(web): cover request() ApiError and empty-body handling (#678) - #1258

Merged
vjuliaife merged 2 commits into
vjuliaife:mainfrom
oluwarantimini:test/678-api-request-errors
Oct 18, 2026
Merged

vjuliaife merged 2 commits into
vjuliaife:mainfrom
oluwarantimini:test/678-api-request-errors

Conversation

@oluwarantimini

@oluwarantimini oluwarantimini commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #678
Closes #665
Closes #677
Closes #672

Adds unit tests for apps/web/lib/api.ts's request(): how it parses response bodies and how it throws ApiError on non-2xx responses.

Changes

apps/web/lib/api-request.test.ts (new)

request() isn't exported, so the tests go through the public api.login, which calls request() with auth: false (no token or localStorage needed). They mock globalThis.fetch, the same pattern as importer-prefetch.test.ts, and restore it after each test.

Test Mock response Assertion
Non-2xx with error body 422 { error: 'validation failed', details: { field: 'amount' } } throws ApiError (also an Error) with status 422, message 'validation failed', details { field: 'amount' }
Non-2xx, empty body 500, no body message falls back to 'HTTP 500', details undefined, no JSON.parse crash
Non-2xx, body without error (extra edge case) 503 { details: { retryAfter: 30 } } message 'HTTP 503', details preserved
2xx 200 with JSON no throw; resolves to the parsed body
Empty-body 2xx 204, no body resolves to null, no JSON.parse crash

Acceptance criteria

  • 422 with {error, details} → ApiError with status 422, message 'validation failed', matching details
  • 500 with an empty body → message falls back to 'HTTP 500'
  • 2xx → no error, parsed body returned
  • Empty-body 2xx (204) → resolves to null, not a JSON.parse crash

Testing

cd apps/web && npx tsx --test lib/api-request.test.ts
ℹ tests 5   ℹ pass 5   ℹ fail 0

Tests only; no production code changes.

…vjuliaife#678)

Mock fetch through api.login (request() is private): a 422 with
{error, details} yields ApiError(422, 'validation failed', details);
an empty-body 500 and an error body without an error field fall back
to 'HTTP <status>'.
@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

Deployment failed for project tariff-shield-web with the following error:

The `vercel.json` schema validation failed with the following message: should NOT have additional property `rootDirectory`

Learn More: https://vercel.com/docs/concepts/projects/project-configuration

@drips-wave

drips-wave Bot commented Oct 18, 2026

Copy link
Copy Markdown

@oluwarantimini Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
tariff-shield-web Error Error Oct 18, 2026 2:09pm UTC

@vjuliaife
vjuliaife merged commit f792c8c into vjuliaife:main Oct 18, 2026
1 of 2 checks passed

This branch had an error being deployed

1 failed deployment
Preview — 5c74cb24 Deployed Oct 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants