mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## 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
117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
import { HttpResponse } from 'msw'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { z } from 'zod'
|
|
|
|
import { getEdgeFunctionsLastHourStats } from './edge-functions-last-hour-stats-query'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
const requestBodySchema = z.object({
|
|
sql: z.string(),
|
|
iso_timestamp_start: z.string(),
|
|
iso_timestamp_end: z.string(),
|
|
})
|
|
|
|
type CapturedRequest = {
|
|
endpoint?: string
|
|
key?: string | null
|
|
body?: z.infer<typeof requestBodySchema>
|
|
}
|
|
|
|
function mockLogsEndpoint(result: unknown[] = []) {
|
|
const captured: CapturedRequest = {}
|
|
for (const endpoint of ['logs.all', 'logs.all.otel'] as const) {
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: `/platform/projects/:ref/analytics/endpoints/${endpoint}`,
|
|
response: async ({ request, params }) => {
|
|
expect(params.ref).toBe('project-ref')
|
|
captured.endpoint = endpoint
|
|
captured.key = new URL(request.url).searchParams.get('key')
|
|
captured.body = requestBodySchema.parse(await request.json())
|
|
return HttpResponse.json({ result })
|
|
},
|
|
})
|
|
}
|
|
return captured
|
|
}
|
|
|
|
describe('getEdgeFunctionsLastHourStats', () => {
|
|
beforeEach(() => {
|
|
// Only fake Date so MSW's own timers keep running.
|
|
vi.useFakeTimers({ toFake: ['Date'] })
|
|
vi.setSystemTime(new Date('2024-01-15T12:00:00.000Z'))
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('requests last-hour function stats from logs.all by default', async () => {
|
|
const captured = mockLogsEndpoint()
|
|
|
|
await getEdgeFunctionsLastHourStats({
|
|
projectRef: 'project-ref',
|
|
functionIds: ['fn_1', 'fn_2'],
|
|
})
|
|
|
|
expect(captured.endpoint).toBe('logs.all')
|
|
expect(captured.key).toBe('last-hour-stats')
|
|
expect(captured.body).toMatchObject({
|
|
iso_timestamp_start: '2024-01-15T11:00:00.000Z',
|
|
iso_timestamp_end: '2024-01-15T12:00:00.000Z',
|
|
})
|
|
expect(captured.body?.sql).toContain(`and function_id in ('fn_1', 'fn_2')`)
|
|
expect(captured.body?.sql).toContain('from\n function_edge_logs')
|
|
})
|
|
|
|
it('requests last-hour function stats from logs.all.otel when useOtel is set', async () => {
|
|
const captured = mockLogsEndpoint()
|
|
|
|
await getEdgeFunctionsLastHourStats({
|
|
projectRef: 'project-ref',
|
|
functionIds: ['fn_1', 'fn_2'],
|
|
useOtel: true,
|
|
})
|
|
|
|
expect(captured.endpoint).toBe('logs.all.otel')
|
|
expect(captured.key).toBe('last-hour-stats')
|
|
expect(captured.body).toMatchObject({
|
|
iso_timestamp_start: '2024-01-15T11:00:00.000Z',
|
|
iso_timestamp_end: '2024-01-15T12:00:00.000Z',
|
|
})
|
|
const sql = captured.body?.sql ?? ''
|
|
expect(sql).toContain(`and log_attributes['function_id'] in ('fn_1', 'fn_2')`)
|
|
expect(sql).toContain('from logs')
|
|
expect(sql).toContain("source = 'function_edge_logs'")
|
|
expect(sql).toContain("case when toInt32OrZero(log_attributes['response.status_code']) >= 500")
|
|
expect(sql).not.toContain('cross join unnest')
|
|
})
|
|
|
|
it('coerces counts to numbers and computes error rates per function', async () => {
|
|
mockLogsEndpoint([
|
|
{ function_id: 'fn_1', requests_count: '100', server_err_count: '5' },
|
|
{ function_id: 'fn_2', requests_count: 8, server_err_count: 0 },
|
|
])
|
|
|
|
const result = await getEdgeFunctionsLastHourStats({
|
|
projectRef: 'project-ref',
|
|
functionIds: ['fn_1', 'fn_2'],
|
|
})
|
|
|
|
expect(result).toEqual({
|
|
fn_1: { functionId: 'fn_1', requestsCount: 100, serverErrorCount: 5, errorRate: 5 },
|
|
fn_2: { functionId: 'fn_2', requestsCount: 8, serverErrorCount: 0, errorRate: 0 },
|
|
})
|
|
})
|
|
|
|
it('skips the logs query when there are no function ids', async () => {
|
|
// No handler registered: an outgoing request would fail the test.
|
|
const result = await getEdgeFunctionsLastHourStats({
|
|
projectRef: 'project-ref',
|
|
functionIds: [],
|
|
})
|
|
|
|
expect(result).toEqual({})
|
|
})
|
|
})
|