Skip to content

Commit

Permalink
wip: ability to choose snapshot file per test case
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 31, 2024
1 parent de9b88e commit 7c95367
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RawSnapshotInfo } from './port/rawSnapshot'
import type { SnapshotResult, SnapshotStateOptions } from './types'
import SnapshotState from './port/state'
import { deepMergeSnapshot } from './port/utils'
import { deepMergeSnapshot, DefaultMap } from './port/utils'

function createMismatchError(
message: string,
Expand Down Expand Up @@ -75,6 +75,27 @@ export class SnapshotClient {
return result
}

private fileToTestIds = new DefaultMap<string, Set<string>>(() => new Set())
private testIdToSnapshotPath: Record<string, string> = {}
private snapshotPathToState = new Map<string, SnapshotState>()

async setupTest(
filepath: string,
testId: string,
options: SnapshotStateOptions,
): Promise<void> {
this.fileToTestIds.get(filepath).add(testId)
const snapshotPath = await options.snapshotEnvironment.resolvePath(filepath)
this.testIdToSnapshotPath[testId] = snapshotPath
if (!this.snapshotPathToState.has(snapshotPath)) {
const content = await options.snapshotEnvironment.readSnapshotFile(snapshotPath)
this.snapshotPathToState.set(
snapshotPath,
new SnapshotState(filepath, snapshotPath, content, options),
)
}
}

skipTest(filepath: string, testName: string): void {
const state = this.getSnapshotState(filepath)
state.markSnapshotsAsCheckedForTest(testName)
Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class SnapshotState {
private updated = new CounterMap<string>()
expand: boolean

private constructor(
constructor(
public testFilePath: string,
public snapshotPath: string,
snapshotContent: string | null,
Expand Down

0 comments on commit 7c95367

Please sign in to comment.