Guides

Testing

Prediction is inherently probabilistic, which is the opposite of what you want in a test. Glacier's test mode makes its behavior fully deterministic.


Test mode

Disable background prediction so the cache only holds what you put there explicitly:

import { Glacier } from '@glacier/core'

export const glacier = new Glacier({
  mode: process.env.NODE_ENV === 'test' ? 'test' : 'auto',
})

In test mode Glacier never warms data on its own — every value comes from an explicit prefetch() or a live fetch — so the same test produces the same result every time.

Asserting on cache behavior

import { test, expect, vi, beforeEach } from 'vitest'
import { glacier } from './glacier.config'

test('serves a warmed entry without fetching', async () => {
  const fetcher = vi.fn(async () => ({ id: 1 }))
  await glacier.prefetch('user:1', { fetcher })

  const value = await glacier.get('user:1')

  expect(value).toEqual({ id: 1 })
  expect(fetcher).toHaveBeenCalledTimes(1) // warmed once, served from cache
})

Resetting between tests

Clear state in a beforeEach so tests don't leak into each other:

beforeEach(() => glacier.clear())

Disable the edge in CI

Edge sync needs a runtime that CI usually doesn't have. Set edge: false in your test config so everything runs in-process.

Previous
Observability