Files
posthog/nodejs/jest.setup-env.ts
Ben Whiteandtests-posthog[bot] 0263530f6d chore(nodejs): parallelize isolated jest tests (#88541)
Co-authored-by: tests-posthog[bot] <250237707+tests-posthog[bot]@users.noreply.github.com>
2026-08-26 11:07:29 +00:00

34 lines
2.0 KiB
TypeScript

import { mkdirSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { brotliDecompressSync } from 'zlib'
// Jest's CLI only sets NODE_ENV=test when it is unset. When tests are launched
// from an environment where NODE_ENV or DEBUG is already set (IDE test runners,
// debuggers, flox exports DEBUG=1), `determineNodeEnv()` resolves to dev and
// every config default (DATABASE_URL, PERSONS_DATABASE_URL, CLICKHOUSE_DATABASE,
// CYCLOTRON_NODE_DATABASE_URL, ...) points at the real dev databases — which
// destructive test helpers would then wipe. Force the test environment so the
// defaults always resolve to their test_* counterparts. Explicitly exported
// *_DATABASE_URL variables still win; tests/helpers/database-guard.ts catches
// those when they point at a non-test database.
process.env.NODE_ENV = 'test'
// Docker development environments export CLICKHOUSE_DATABASE=posthog. Tests that
// reset ClickHouse must never inherit it, even when it was explicitly exported.
process.env.CLICKHOUSE_DATABASE = 'posthog_test'
// Tests must never read the live GeoLite2 database from share/ — it is re-downloaded
// unpinned from mmdbcdn.posthog.net and its data changes under us (see the recurring
// postal-code snapshot drift). Point every GeoIPService at MaxMind's frozen test
// database instead, so lookups are deterministic. Use IPs from the test ranges
// (e.g. 89.160.20.129 → Linköping, 216.160.83.56 → Milton) in tests.
const fixturePath = join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')
const mmdbPath = join(__dirname, '.tmp', `GeoLite2-City-Test-${process.env.JEST_WORKER_ID ?? 'main'}.mmdb`)
mkdirSync(join(__dirname, '.tmp'), { recursive: true })
writeFileSync(mmdbPath, brotliDecompressSync(readFileSync(fixturePath)))
// Sidecar metadata file, so GeoIPService doesn't warn about a missing one on every load
writeFileSync(mmdbPath.replace('.mmdb', '.json'), JSON.stringify({ date: '2025-01-01' }))
process.env.MMDB_FILE_LOCATION = mmdbPath