Skip to content

Test Runner: custom parser for snapshot #57830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
axetroy opened this issue Apr 11, 2025 · 1 comment
Open

Test Runner: custom parser for snapshot #57830

axetroy opened this issue Apr 11, 2025 · 1 comment
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.

Comments

@axetroy
Copy link

axetroy commented Apr 11, 2025

What is the problem this feature will solve?

Currently, snapshots have a special format and appear to be cjs modules

It is very convenient for snapshot parsing, but it is not suitable for human reading. It does not highlight.

I would like to have a way to customize the snapshot, since I prefer .snapshot.md to .snapshot format.

What is the feature you are proposing to solve the problem?

Add a custom parse to custom snapshot's format.

Definition:

declare module "snapshot" {
  /**
   * Sets the snapshot parser to be used for parsing and taking snapshots.
   * @param parser
   */
  export function setSnapshotParser(parser: SnapshotParser): void;

  /**
   * Interface representing a snapshot parser.
   */
  interface SnapshotParser {
    /**
     * Resolve the path to the snapshot file.
     * @param filePath
     */
    resolve(filePath: string): string;

    /**
     * Parses a snapshot file and returns its contents as a Map.
     * @param snapshotFilePath - The path to the snapshot file.
     * @returns The parsed snapshot as a Map.
     */
    parse(snapshotFilePath: string): Map<string, string>;

    /**
     * Takes a snapshot of the given value and saves it to the specified file.
     * @param value
     */
    snapshot(value: unknown): void;
  }
}

Usage:

In this example, we use custom markdown as a snapshot

snapshot.setSnapshotParser({
	// ✅ Use the markdown format for snapshots
	resolve: (filePath: string) => {
		return filePath + '.snapshot.md'
	},
	// ✅ Parse the snapshot file and return its contents as a Map
	parse: (snapshotFilePath: string) => {
		// Implement your parsing logic here
		return new Map<string, string>()
	},
	// ✅ Take a snapshot of the given value and save it to the specified file
	snapshot: (value: unknown) => {
		// Implement your snapshot logic here
	}
})

What alternatives have you considered?

No response

@axetroy axetroy added the feature request Issues that request new features to be added to Node.js. label Apr 11, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Apr 11, 2025
@marco-ippolito marco-ippolito added the test_runner Issues and PRs related to the test runner subsystem. label Apr 11, 2025
@cjihrig
Copy link
Contributor

cjihrig commented Apr 11, 2025

You can already customize how snapshots are serialized by default, as well as per call. You can also customize where the snapshots are written. There is also the fileSnapshot() API.

I'm not sure we need much more customization than that. If so, you can also register your own snapshot assertions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

3 participants