Skip to content

Commit 7335a23

Browse files
chore: improve integration test infrastructure (#2126)
## Summary Currently it's hard to debug integration tests because of all the extra logging/warnings/open handler issues (wasn't sure if I introduced an open handler or was existing). This fixes that. - Bump Jest from v28 to v30 in api and common-utils packages, syncing with app's existing setup - Replace legacy `preset: 'ts-jest'` with modern `createJsWithTsPreset()` spread pattern across all packages - Fix open handles that required `--forceExit` and `--detectOpenHandles` workarounds - Suppress noisy console and logger output during test runs via targeted mocks ## Changes ### Jest/ts-jest upgrade - Bump `jest` 28 → 30, `@types/jest` 28 → 29 in api and common-utils - Adopt `createJsWithTsPreset()` config pattern (matching app) - Add `isolatedModules: true` to common-utils tsconfig to fix ts-jest warning with Node16 module kind - Update snapshot files and inline snapshots for Jest 30 format changes - Replace removed `toThrowError()` with `toThrow()` - Fix team.test.ts inline snapshot that depended on dynamic Mongo ObjectIds ### Open handle fixes - Add `close()` method to `BaseClickhouseClient` in common-utils - Call `mongoose.disconnect()` in `closeDB()` (api fixtures) - Add `closeTestFixtureClickHouseClient()` and call it in `MockServer.stop()` - Fix common-utils integration tests to close both `hdxClient` and raw `client` in `afterAll` - Disable `usageStats()` interval in CI to prevent leaked timers - Remove `--forceExit` from common-utils CI/dev scripts and api dev script - Remove `--detectOpenHandles` from all dev scripts ### Log noise suppression - Use `jest.spyOn` for console methods instead of global console object override - Add per-file `console.warn`/`console.error` suppression in test files that exercise error paths - Mock pino logger module in api jest.setup.ts to suppress expected operational logs (validation errors, MCP tool errors, etc.) - Use pino logger instead of `console.error` in Express error handler (`middleware/error.ts`) - Add console suppression to app setupTests.tsx ## Testing - `make ci-lint` — passes - `make ci-unit` — 2177 tests pass, zero console noise - `make ci-int` — 642 tests pass (606 api + 36 common-utils), zero log noise
1 parent 0a4fb15 commit 7335a23

29 files changed

Lines changed: 410 additions & 1375 deletions

‎packages/api/.env.test‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ MONGO_URI=mongodb://localhost:${HDX_CI_MONGO_PORT:-39999}/hyperdx-test
1212
NODE_ENV=test
1313
PORT=${HDX_CI_API_PORT:-19000}
1414
OPAMP_PORT=${HDX_CI_OPAMP_PORT:-14320}
15-
# Default to only logging warnings/errors. Adjust if you need more verbosity
16-
HYPERDX_LOG_LEVEL=warn
15+
# Default to only logging errors. Adjust if you need more verbosity.
16+
# Note: the logger module is mocked in jest.setup.ts to suppress expected
17+
# operational noise (validation errors, MCP tool errors, etc.) during tests.
18+
HYPERDX_LOG_LEVEL=error

‎packages/api/jest.config.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
1+
const { createJsWithTsPreset } = require('ts-jest');
2+
3+
const tsJestTransformCfg = createJsWithTsPreset();
4+
5+
/** @type {import("jest").Config} **/
26
module.exports = {
7+
...tsJestTransformCfg,
38
setupFilesAfterEnv: ['<rootDir>/../jest.setup.ts'],
49
setupFiles: ['dotenv-expand/config'],
5-
preset: 'ts-jest',
610
testEnvironment: 'node',
711
verbose: true,
812
rootDir: './src',

‎packages/api/jest.setup.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
// @eslint-disable @typescript-eslint/no-var-requires
21
jest.retryTimes(1, { logErrorsBeforeRetry: true });
32

4-
global.console = {
5-
...console,
6-
// Turn off noisy console logs in tests
7-
debug: jest.fn(),
8-
info: jest.fn(),
9-
};
3+
// Suppress noisy console output during test runs.
4+
// - debug/info: ClickHouse query logging, server startup messages
5+
// - warn: expected column-not-found warnings from renderChartConfig on CTE tables
6+
jest.spyOn(console, 'debug').mockImplementation(() => {});
7+
jest.spyOn(console, 'info').mockImplementation(() => {});
8+
jest.spyOn(console, 'warn').mockImplementation(() => {});
109

1110
// Mock alert notification functions to prevent HTTP calls during tests
1211
jest.mock('@/utils/slack', () => ({

‎packages/api/package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@
5858
"@types/cors": "^2.8.14",
5959
"@types/express": "^4.17.13",
6060
"@types/express-session": "^1.17.7",
61-
"@types/jest": "^28.1.1",
61+
"@types/jest": "^29.5.14",
6262
"@types/lodash": "^4.14.198",
6363
"@types/minimist": "^1.2.2",
6464
"@types/ms": "^0.7.31",
6565
"@types/passport-local": "^1.0.34",
6666
"@types/supertest": "^2.0.12",
6767
"@types/swagger-jsdoc": "^6",
6868
"@types/uuid": "^8.3.4",
69-
"jest": "^28.1.3",
69+
"jest": "^30.2.0",
7070
"migrate-mongo": "^11.0.0",
7171
"nodemon": "^2.0.20",
7272
"pino-pretty": "^13.1.1",
@@ -90,7 +90,7 @@
9090
"lint:fix": "npx eslint . --ext .ts --fix",
9191
"ci:lint": "yarn lint && yarn tsc --noEmit && yarn lint:openapi",
9292
"ci:int": "DOTENV_CONFIG_PATH=.env.test DOTENV_CONFIG_OVERRIDE=true jest --runInBand --ci --forceExit --coverage",
93-
"dev:int": "DOTENV_CONFIG_PATH=.env.test DOTENV_CONFIG_OVERRIDE=true jest --runInBand --forceExit --coverage",
93+
"dev:int": "DOTENV_CONFIG_PATH=.env.test DOTENV_CONFIG_OVERRIDE=true jest --runInBand --coverage",
9494
"dev:migrate-db-create": "ts-node node_modules/.bin/migrate-mongo create -f migrate-mongo-config.ts",
9595
"dev:migrate-db": "ts-node node_modules/.bin/migrate-mongo up -f migrate-mongo-config.ts",
9696
"dev:migrate-ch-create": "migrate create -ext sql -dir ./migrations/ch -seq",

‎packages/api/src/api-app.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ app.use(defaultCors);
8080
// ---------------------------------------------------------------------
8181
// ----------------------- Background Jobs -----------------------------
8282
// ---------------------------------------------------------------------
83-
if (config.USAGE_STATS_ENABLED) {
83+
if (config.USAGE_STATS_ENABLED && !config.IS_CI) {
8484
usageStats();
8585
}
8686
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)