diff --git a/package.json b/package.json index d4c50331..19f43484 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "format": "pretty-quick --staged", "format:all": "prettier --write .", "test": "npm run test:unit", - "test:unit": "node --import ./tests/unit/env.js --test-isolation=none --test-force-exit --test 'tests/unit/specs/**/*.test.js'", + "test:unit": "node --import ./tests/unit/env.js --test-isolation=none --test-force-exit --test-reporter=./tests/unit/testReporter.js --test 'tests/unit/specs/**/*.test.js'", "test:unit:coverage": "node --import ./tests/unit/env.js --test-isolation=none --test-force-exit --experimental-test-coverage --test-coverage-include='src/js/**' --test 'tests/unit/specs/**/*.test.js'", "test:e2e": "npx playwright test", "test:visual": "VISUAL_CAPTURE_DIR=tests/.artifacts/visual npx playwright test tests/e2e/specs/views", diff --git a/tests/unit/testReporter.js b/tests/unit/testReporter.js new file mode 100644 index 00000000..4ad3aab8 --- /dev/null +++ b/tests/unit/testReporter.js @@ -0,0 +1,25 @@ +import { compose } from "node:stream"; +import { spec } from "node:test/reporters"; + +const THRESHOLD_MS = Number(process.env.SLOW_TEST_MS ?? 500); + +// Custom node:test reporter to record slow tests (>500ms by default) +export default async function* testReporter(source) { + let slowCount = 0; + const trackSlowTests = async function* () { + for await (const event of source) { + if (event.type === "test:pass" || event.type === "test:fail") { + const { details } = event.data; + if (details?.type !== "suite" && details?.duration_ms >= THRESHOLD_MS) { + slowCount++; + } + } + yield event; + } + yield { + type: "test:diagnostic", + data: { nesting: 0, message: `slow ${slowCount}` }, + }; + }; + yield* compose(trackSlowTests(), spec); +}