"use strict";
const { createHash } = require("node:crypto");
const { mkdirSync, renameSync, writeFileSync } = require("node:fs");
const { relative, resolve, sep } = require("node:path");
const runtime =
globalThis.__SUPERCOV_DIRECT_RUNTIME__ ??
(typeof process !== "undefined" ? process.__SUPERCOV_DIRECT_RUNTIME__ : undefined);
const evidenceDirectory = process.env.SUPERCOV_EVIDENCE_DIR;
function digest(value) {
return createHash("sha256").update(value).digest("hex").slice(0, 24);
}
function localFile(path) {
return relative(process.cwd(), path).split(sep).join("/");
}
function testIdentity(testFile, fullName) {
return `jest:${digest(`${testFile}\0${fullName}`)}`;
}
const KINDS = [
["unit", /(^|[/_.-])unit([/_.-]|$)/i],
["component", /(^|[/_.-])(component|components|ct)([/_.-]|$)/i],
["integration", /(^|[/_.-])(integration|int)([/_.-]|$)/i],
["e2e", /(^|[/_.-])(e2e|end-to-end|offline|online)([/_.-]|$)/i],
];
function provenance(testFile) {
const explicit = process.env.SUPERCOV_TEST_KIND?.trim();
if (explicit)
return { runner: "jest", kind: explicit.toLowerCase(), source: "explicit" };
const kind = KINDS.find(([, pattern]) => pattern.test(testFile))?.[0];
return kind
? { runner: "jest", kind, source: "path" }
: { runner: "jest", kind: "unit", source: "runner-default" };
}
function writeEvidence(suffix, payload) {
const directory = resolve(process.cwd(), evidenceDirectory, suffix);
mkdirSync(directory, { recursive: true });
const target = resolve(directory, "mcdc.json");
const temporary = `${target}.${process.pid}.${Date.now()}.tmp`;
writeFileSync(temporary, `${JSON.stringify(payload)}\n`);
renameSync(temporary, target);
}
if (runtime && evidenceDirectory && typeof beforeEach === "function" && typeof afterEach === "function") {
runtime.enableRuntimeSnapshotEvidence();
const attempts = new Map();
const emittedSetupFiles = new Set();
let active;
beforeEach(() => {
const state = expect.getState();
const testFile = localFile(state.testPath ?? "unknown");
if (!emittedSetupFiles.has(testFile)) {
emittedSetupFiles.add(testFile);
const setupSnapshot = runtime.coverageSnapshot();
if (setupSnapshot.hits.length || setupSnapshot.decisions.length) {
writeEvidence(`jest-${digest(testFile)}-setup`, {
testId: `jest:${digest(testFile)}:setup`,
test: `${testFile} > module setup`,
testFile,
title: "module setup",
retry: 0,
status: "passed",
provenance: provenance(testFile),
role: "setup",
runtime: [setupSnapshot],
browser: [],
server: [],
});
}
}
const fullName = state.currentTestName ?? "test";
const testId = testIdentity(testFile, fullName);
const retry = attempts.get(testId) ?? 0;
attempts.set(testId, retry + 1);
const testKey = digest(testId);
const scope = {
version: 1,
runId: process.env.SUPERCOV_RUN_ID ?? "unscoped",
workerId: `jest-${process.env.JEST_WORKER_ID ?? process.pid}`,
testId,
testKey,
retry,
attemptId: `${testKey}-${retry}`,
};
active = { scope, testFile, fullName, retry };
runtime.activateCoverageScope(scope);
runtime.resetCoverage(testId);
});
afterEach(() => {
const current = active;
active = undefined;
if (!current)
return;
const { scope } = current;
writeEvidence(`jest-${scope.attemptId}`, {
testId: scope.testId,
scope,
test: current.fullName,
testFile: current.testFile,
title: current.fullName,
retry: current.retry,
status: "unknown",
provenance: provenance(current.testFile),
phases: runtime.takeNodeAssertionPhases(scope),
runtime: [runtime.coverageSnapshot()],
browser: [],
server: [],
});
runtime.resetCoverage();
runtime.activateCoverageScope();
});
}