-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ff9854
commit ebee8f2
Showing
2 changed files
with
71 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, Map<string, number>>() | ||
const snapshots = new Map<string, Record<string, string>>() | ||
const counters = new Map<string, Map<string, number>>(); | ||
const snapshots = new Map<string, Record<string, string>>(); | ||
|
||
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<string, number>() | ||
counters.set(file, counter) | ||
const count = counter.get(testName) ?? 1 | ||
counter.set(testName, count + 1) | ||
const counter = counters.get(file) ?? new Map<string, number>(); | ||
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, | ||
}; | ||
} |