mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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
191 lines
5.4 KiB
TypeScript
191 lines
5.4 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
checkHasNonPositiveValues,
|
|
formatLogTick,
|
|
getCumulativeResults,
|
|
} from '@/components/ui/QueryBlock/QueryBlock.utils'
|
|
|
|
describe('checkHasNonPositiveValues', () => {
|
|
it('returns false for an empty array', () => {
|
|
expect(checkHasNonPositiveValues([], 'value')).toBe(false)
|
|
})
|
|
|
|
it('returns false when all values are positive', () => {
|
|
const data = [{ value: 1 }, { value: 2 }, { value: 100 }]
|
|
expect(checkHasNonPositiveValues(data, 'value')).toBe(false)
|
|
})
|
|
|
|
it('returns true when a value is zero', () => {
|
|
const data = [{ value: 1 }, { value: 0 }, { value: 3 }]
|
|
expect(checkHasNonPositiveValues(data, 'value')).toBe(true)
|
|
})
|
|
|
|
it('returns true when a value is negative', () => {
|
|
const data = [{ value: 5 }, { value: -1 }, { value: 3 }]
|
|
expect(checkHasNonPositiveValues(data, 'value')).toBe(true)
|
|
})
|
|
|
|
it('checks only the specified key', () => {
|
|
const data = [
|
|
{ x: -1, y: 5 },
|
|
{ x: 2, y: 10 },
|
|
]
|
|
expect(checkHasNonPositiveValues(data, 'y')).toBe(false)
|
|
expect(checkHasNonPositiveValues(data, 'x')).toBe(true)
|
|
})
|
|
|
|
it('returns false when key is absent (undefined cast to NaN is not <= 0)', () => {
|
|
const data = [{ value: 1 }, { value: 2 }]
|
|
expect(checkHasNonPositiveValues(data, 'missing')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('formatLogTick', () => {
|
|
it('formats values below 1,000 as plain locale strings', () => {
|
|
expect(formatLogTick(0)).toBe('0')
|
|
expect(formatLogTick(1)).toBe('1')
|
|
expect(formatLogTick(999)).toBe('999')
|
|
expect(formatLogTick(500)).toBe('500')
|
|
})
|
|
|
|
it('formats values >= 1,000 with a "k" suffix', () => {
|
|
expect(formatLogTick(1_000)).toBe('1k')
|
|
expect(formatLogTick(1_500)).toBe('1.5k')
|
|
expect(formatLogTick(10_000)).toBe('10k')
|
|
expect(formatLogTick(999_999)).toBe('1,000k')
|
|
})
|
|
|
|
it('formats values >= 1,000,000 with an "M" suffix', () => {
|
|
expect(formatLogTick(1_000_000)).toBe('1M')
|
|
expect(formatLogTick(1_500_000)).toBe('1.5M')
|
|
expect(formatLogTick(10_000_000)).toBe('10M')
|
|
expect(formatLogTick(1_234_567)).toBe('1.2M')
|
|
})
|
|
|
|
it('respects maximumFractionDigits of 1', () => {
|
|
// 1,050 → 1.05k, but max 1 decimal → "1.1k" (rounded)
|
|
expect(formatLogTick(1_050)).toBe('1.1k')
|
|
// 1,049 → 1.049k → "1k" (rounded down)
|
|
expect(formatLogTick(1_049)).toBe('1k')
|
|
})
|
|
})
|
|
|
|
describe('getCumulativeResults', () => {
|
|
it('returns empty array when results are empty', () => {
|
|
expect(getCumulativeResults({ rows: [] }, { yKey: 'y' })).toEqual([])
|
|
})
|
|
|
|
it('returns empty array when results are undefined', () => {
|
|
expect(getCumulativeResults(undefined as any, { yKey: 'y' })).toEqual([])
|
|
})
|
|
|
|
it('accumulates yKey values across rows', () => {
|
|
const results = {
|
|
rows: [
|
|
{ x: 'a', y: 10 },
|
|
{ x: 'b', y: 20 },
|
|
{ x: 'c', y: 5 },
|
|
],
|
|
}
|
|
const config = {
|
|
type: 'bar' as const,
|
|
xKey: 'x',
|
|
yKey: 'y',
|
|
cumulative: true,
|
|
showLabels: false,
|
|
showGrid: false,
|
|
}
|
|
const output = getCumulativeResults(results, config)
|
|
expect(output).toEqual([
|
|
{ x: 'a', y: 10 },
|
|
{ x: 'b', y: 30 },
|
|
{ x: 'c', y: 35 },
|
|
])
|
|
})
|
|
|
|
it('accumulates each key independently when yKey is an array', () => {
|
|
const results = {
|
|
rows: [
|
|
{ x: 'a', y1: 10, y2: 1 },
|
|
{ x: 'b', y1: 20, y2: 2 },
|
|
{ x: 'c', y1: 5, y2: 3 },
|
|
],
|
|
}
|
|
const output = getCumulativeResults(results, { yKey: ['y1', 'y2'] })
|
|
expect(output).toEqual([
|
|
{ x: 'a', y1: 10, y2: 1 },
|
|
{ x: 'b', y1: 30, y2: 3 },
|
|
{ x: 'c', y1: 35, y2: 6 },
|
|
])
|
|
})
|
|
|
|
it('preserves other keys on each row', () => {
|
|
const results = {
|
|
rows: [
|
|
{ x: 'a', y: 1, label: 'foo' },
|
|
{ x: 'b', y: 2, label: 'bar' },
|
|
],
|
|
}
|
|
const config = {
|
|
type: 'bar' as const,
|
|
xKey: 'x',
|
|
yKey: 'y',
|
|
cumulative: true,
|
|
showLabels: false,
|
|
showGrid: false,
|
|
}
|
|
const output = getCumulativeResults(results, config)
|
|
expect(output[0]).toMatchObject({ x: 'a', y: 1, label: 'foo' })
|
|
expect(output[1]).toMatchObject({ x: 'b', y: 3, label: 'bar' })
|
|
})
|
|
|
|
// Postgres returns `bigint`, `numeric`, `money` and `count(*)` columns as
|
|
// strings, so the running total must sum numerically rather than concatenate.
|
|
it('sums numeric string yKey values instead of concatenating them', () => {
|
|
const results = {
|
|
rows: [
|
|
{ x: 'a', y: '10' },
|
|
{ x: 'b', y: '20' },
|
|
{ x: 'c', y: '30' },
|
|
],
|
|
}
|
|
const config = {
|
|
type: 'bar' as const,
|
|
xKey: 'x',
|
|
yKey: 'y',
|
|
cumulative: true,
|
|
showLabels: false,
|
|
showGrid: false,
|
|
}
|
|
const output = getCumulativeResults(results, config)
|
|
expect(output).toEqual([
|
|
{ x: 'a', y: 10 },
|
|
{ x: 'b', y: 30 },
|
|
{ x: 'c', y: 60 },
|
|
])
|
|
})
|
|
|
|
it('treats null/undefined and non-numeric yKey values as 0', () => {
|
|
const results = {
|
|
rows: [
|
|
{ x: 'a', y: null },
|
|
{ x: 'b', y: '5' },
|
|
{ x: 'c', y: undefined },
|
|
{ x: 'd', y: 'not-a-number' },
|
|
{ x: 'e', y: 4 },
|
|
],
|
|
}
|
|
const config = {
|
|
type: 'bar' as const,
|
|
xKey: 'x',
|
|
yKey: 'y',
|
|
cumulative: true,
|
|
showLabels: false,
|
|
showGrid: false,
|
|
}
|
|
const output = getCumulativeResults(results, config)
|
|
expect(output.map((row: { y: number }) => row.y)).toEqual([0, 5, 5, 5, 9])
|
|
})
|
|
})
|