diff --git a/packages/earl/src/validators/snapshots/TestContext.ts b/packages/earl/src/validators/snapshots/TestContext.ts index 70251f28..2676f43a 100644 --- a/packages/earl/src/validators/snapshots/TestContext.ts +++ b/packages/earl/src/validators/snapshots/TestContext.ts @@ -1,47 +1,51 @@ -export type TestContext = NodeTestContext | MochaTestContext | UvuTestContext +export type TestContext = NodeTestContext | MochaTestContext | UvuTestContext; export interface NodeTestContext { - name: string + name: string; } export interface MochaTestContext { test?: { - file?: string - fullTitle(): string - } + file?: string; + fullTitle(): string; + }; } export interface UvuTestContext { - __test__?: string - __suite__?: string + __test__?: string; + __suite__?: string; } export function getTestFile(context: TestContext): string | undefined { - if ('test' in context) { - const file = context.test?.file - if (typeof file === 'string') { - return file + console.trace("getTestFile", { context: JSON.stringify(context, null, 2) }); + if ("test" in context) { + const file = context.test?.file; + if (typeof file === "string") { + console.log({ file }); + return file; } } + + console.log("Returning undefined"); } export function getTestName(context: TestContext): string | undefined { - if ('test' in context) { - if (typeof context.test?.fullTitle === 'function') { - const title = context.test.fullTitle() - if (typeof title === 'string') { - return title + if ("test" in context) { + if (typeof context.test?.fullTitle === "function") { + const title = context.test.fullTitle(); + if (typeof title === "string") { + return title; } } } - if ('__test__' in context) { - const parts = [context.__test__] + if ("__test__" in context) { + const parts = [context.__test__]; if (context.__suite__) { - parts.unshift(context.__suite__) + parts.unshift(context.__suite__); } - return parts.filter((x) => x !== undefined).join(' ') + return parts.filter((x) => x !== undefined).join(" "); } - if ('name' in context && typeof context.name === 'string') { - return context.name + if ("name" in context && typeof context.name === "string") { + return context.name; } } diff --git a/packages/earl/src/validators/snapshots/getSnapshot.ts b/packages/earl/src/validators/snapshots/getSnapshot.ts index fbff344a..65ac2bee 100644 --- a/packages/earl/src/validators/snapshots/getSnapshot.ts +++ b/packages/earl/src/validators/snapshots/getSnapshot.ts @@ -1,60 +1,75 @@ -import { readFileSync } from 'node:fs' -import path from 'node:path' +import { readFileSync } from "node:fs"; +import path from "node:path"; -import { type TestContext, getTestFile, getTestName } from './TestContext.js' -import { parseSnapshot } from './format.js' -import type { SnapshotUpdateMode } from './getSnapshotUpdateMode.js' +import { type TestContext, getTestFile, getTestName } from "./TestContext.js"; +import { parseSnapshot } from "./format.js"; +import type { SnapshotUpdateMode } from "./getSnapshotUpdateMode.js"; -const counters = new Map>() -const snapshots = new Map>() +const counters = new Map>(); +const snapshots = new Map>(); export function resetSnapshotCache() { - counters.clear() - snapshots.clear() + counters.clear(); + snapshots.clear(); } export function getSnapshot( controlFileName: string | undefined, context: TestContext, - mode: SnapshotUpdateMode, + mode: SnapshotUpdateMode ) { - const filePath = getTestFile(context) ?? controlFileName + console.trace("getSnapshot", { + controlFileName, + context: JSON.stringify(context, null, 2), + mode: JSON.stringify(mode, null, 2), + }); + + const filePath = getTestFile(context) ?? controlFileName; + + console.log({ filePath }); + if (!filePath) { throw new TypeError( - 'Invalid test context. Cannot determine test file path.', - ) + "Invalid test context. Cannot determine test file path." + ); } const file = path.join( path.dirname(filePath), - `${path.basename(filePath)}.snapshot`, - ) - const testName = getTestName(context) + `${path.basename(filePath)}.snapshot` + ); + + console.log({ file }); + + const testName = getTestName(context); + + console.log({ testName }); + if (!testName) { - throw new TypeError('Invalid test context. Cannot determine test name.') + throw new TypeError("Invalid test context. Cannot determine test name."); } - const counter = counters.get(file) ?? new Map() - counters.set(file, counter) - const count = counter.get(testName) ?? 1 - counter.set(testName, count + 1) + const counter = counters.get(file) ?? new Map(); + counters.set(file, counter); + const count = counter.get(testName) ?? 1; + counter.set(testName, count + 1); - const name = `${testName} ${count}` + const name = `${testName} ${count}`; - let content = snapshots.get(file) - if (!content || mode === 'all') { + let content = snapshots.get(file); + if (!content || mode === "all") { try { - content = parseSnapshot(readFileSync(file, 'utf8')) + content = parseSnapshot(readFileSync(file, "utf8")); } catch {} } - content = content ?? {} - snapshots.set(file, content) + content = content ?? {}; + snapshots.set(file, content); - const expected = content[name] + const expected = content[name]; return { file, name, content, - expected: typeof expected === 'string' ? expected : undefined, - } + expected: typeof expected === "string" ? expected : undefined, + }; }