Skip to content

Commit

Permalink
Measure test time
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 30, 2021
1 parent c385768 commit dd53c3c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions test/jest-light-runner/src/worker-runner.js
@@ -1,12 +1,13 @@
import path from "path";
import { fileURLToPath, pathToFileURL } from "url";
import { performance } from "perf_hooks";
import snapshot from "jest-snapshot";
import expect from "expect";
import * as circus from "jest-circus";

import "./global-setup.js";

/** @typedef {{ failures: number, passses: number, pending: number }} Stats */
/** @typedef {{ failures: number, passses: number, pending: number, start: number, end: number }} Stats */
/** @typedef {{ ancestors: string[], title: string, errors: Error[], skipped: boolean }} InternalTestResult */

// Node.js workers (worker_therads) don't support
Expand All @@ -26,7 +27,7 @@ export default async function ({ test, updateSnapshot, port }) {
port.postMessage("start");

/** @type {Stats} */
const stats = { passes: 0, failures: 0, pending: 0 };
const stats = { passes: 0, failures: 0, pending: 0, start: 0, end: 0 };
/** @type {Array<InternalTestResult>} */
const results = [];

Expand All @@ -38,7 +39,9 @@ export default async function ({ test, updateSnapshot, port }) {
);
expect.setState({ snapshotState });

stats.start = performance.now();
await runTestBlock(tests, hasFocusedTests, results, stats);
stats.end = performance.now();

snapshotState._inlineSnapshots.forEach(({ frame }) => {
// When using native ESM, errors have an URL location.
Expand All @@ -47,7 +50,7 @@ export default async function ({ test, updateSnapshot, port }) {
});
snapshotState.save();

return toTestResult(stats, results, test.path);
return toTestResult(stats, results, test);
}

async function loadTests(testFile) {
Expand Down Expand Up @@ -160,10 +163,13 @@ function callAsync(fn) {
*
* @param {Stats} stats
* @param {Array<InternalTestResult>} tests
* @param {string} path
* @param {import("@jest/test-result").Test} testInput
* @returns {import("@jest/test-result").TestResult}
*/
function toTestResult(stats, tests, path) {
function toTestResult(stats, tests, { path, context }) {
const { start, end } = stats;
const runtime = end - start;

return {
coverage: globalThis.__coverage__,
console: null,
Expand All @@ -175,8 +181,10 @@ function toTestResult(stats, tests, path) {
numPassingTests: stats.passes,
numPendingTests: stats.pending,
perfStats: {
end: +new Date(stats.end),
start: +new Date(stats.start),
start,
end,
runtime: Math.round(runtime), // ms precision
slow: runtime / 1000 > context.config.slowTestThreshold,
},
skipped: false,
snapshot: {
Expand Down

0 comments on commit dd53c3c

Please sign in to comment.