Files
supabase/apps/studio/data/normalizeEmptyBodyResponse.test.ts
T
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## Context

_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_

We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.

## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail 
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files 
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
2026-10-23 18:51:27 +08:00

88 lines
3.2 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() }))
vi.mock('common', () => ({ IS_PLATFORM: false, getAccessToken: vi.fn() }))
vi.mock('@/lib/constants', () => ({ API_URL: 'http://localhost' }))
vi.mock('@/lib/helpers', () => ({ uuidv4: () => 'test-uuid' }))
// Import after mocks are set up
const { normalizeEmptyBodyResponse, client } = await import('./fetchers')
describe('normalizeEmptyBodyResponse', () => {
it('adds Content-Length: 0 to an empty 201 that omits it (HTTP/3 case)', async () => {
// HTTP/3 responses can drop `Content-Length: 0` on empty bodies, which makes
// openapi-fetch attempt to JSON-parse the empty body and throw.
const response = new Response(null, {
status: 201,
statusText: 'Created',
headers: { 'X-Request-Id': 'req-123' },
})
expect(response.headers.has('Content-Length')).toBe(false)
const normalized = await normalizeEmptyBodyResponse(response)
expect(normalized.status).toBe(201)
expect(normalized.statusText).toBe('Created')
expect(normalized.headers.get('X-Request-Id')).toBe('req-123')
expect(normalized.headers.get('Content-Length')).toBe('0')
// The normalized empty body must not throw when parsed the way openapi-fetch parses it.
expect(await normalized.text()).toBe('')
})
it('leaves a response that already has Content-Length untouched (HTTP/2 case)', async () => {
const response = new Response(null, {
status: 201,
headers: { 'Content-Length': '0' },
})
const normalized = await normalizeEmptyBodyResponse(response)
expect(normalized).toBe(response)
})
it('does not modify a non-empty body that omits Content-Length', async () => {
const response = new Response(JSON.stringify({ id: 1 }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
expect(response.headers.has('Content-Length')).toBe(false)
const normalized = await normalizeEmptyBodyResponse(response)
// Returns the original response (body still readable, not consumed by the peek).
expect(normalized).toBe(response)
expect(await normalized.json()).toEqual({ id: 1 })
})
it('leaves 204 No Content untouched even without Content-Length', async () => {
const response = new Response(null, { status: 204 })
const normalized = await normalizeEmptyBodyResponse(response)
expect(normalized).toBe(response)
})
})
describe('openapi-fetch client — empty 201 without Content-Length (HTTP/3)', () => {
afterEach(() => vi.unstubAllGlobals())
it('resolves with data instead of throwing on JSON parse', async () => {
// Simulate the HTTP/3 transport: a 201 with an empty body and no Content-Length.
// Without normalization openapi-fetch calls response.json() and throws
// "Unexpected end of JSON input"; the middleware should make this succeed.
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(null, { status: 201 }))
)
const { data, error, response } = await client.POST('/v1/projects/{ref}/secrets' as any, {
params: { path: { ref: 'test-ref' } },
body: [] as any,
})
expect(error).toBeUndefined()
expect(data).toEqual({})
expect(response.status).toBe(201)
})
})