Skip to content

Snapshot.Match — support configurable snapshot root independent of source file location #9654

@dominik-niebuhr

Description

@dominik-niebuhr

Product

Hot Chocolate

Is your feature request related to a problem?

Snapshot.Match resolves the snapshot directory by walking the call-site StackTrace to derive
the test file's source path, then appending __snapshots__/. This breaks when the canonical
snapshot file lives outside the project directory. In a monorepo it's common to keep shared
artifacts at a top-level path (e.g. schemas/graphql/service.graphqls at repo root) that is
intentionally shared across consumers. The test project lives at cloud/my-service/tests/, so
the source-derived path resolves to somewhere under there — not to schemas/.

Because there is no way to override the resolved path, we could not use Snapshot.Match for a
GraphQL schema snapshot test that needed to compare against schemas/graphql/service.graphqls.
We had to reimplement the pattern manually: read via Assembly.GetManifestResourceStream and write
the mismatch to an AppContext-injected path on failure.

The solution you'd like

Two complementary overloads/settings would cover the main cases:

1 — explicit expected value overload

Snapshot.Match(actual, expected);

When expected is supplied explicitly, skip all StackFrame-based resolution. On mismatch,
write actual to the mismatch path and produce the usual diff in the assertion failure message.

2 — configurable snapshot root and mismatch directory

// global default (e.g. from AppContext / runtimeconfig.json)
SnapshotSettings.Default.SnapshotRootDirectory = "/repo/schemas/graphql";
SnapshotSettings.Default.MismatchDirectory     = "/repo/schemas/graphql/__mismatch__";

// or per-call
Snapshot.Match(actual, s => s.SnapshotRootDirectory("/repo/schemas/graphql"));

Resolve .snap relative to this directory instead of the source file. The mismatch write
on failure follows the same configured path rather than a source-tree-relative one.

Either option would let us remove the manual boilerplate we have today and use Snapshot.Match
directly, while keeping the auto-write-on-fail UX that makes snapshot tests practical.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions