Skip to content
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

feat(expect): support expect.addSnapshotSerialize() api #4537

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions expect/_snapshot_serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import type { SnapshotPlugin } from "./_types.ts";

let INTERNAL_PLUGINS: SnapshotPlugin[] = [
// Todo: support serialize plugin
];

export function addSerializer(plugin: SnapshotPlugin): void {
INTERNAL_PLUGINS = [plugin, ...INTERNAL_PLUGINS];
}

Check warning on line 11 in expect/_snapshot_serializer.ts

View check run for this annotation

Codecov / codecov/patch

expect/_snapshot_serializer.ts#L9-L11

Added lines #L9 - L11 were not covered by tests

export function getSerializer() {
return INTERNAL_PLUGINS;
}

Check warning on line 15 in expect/_snapshot_serializer.ts

View check run for this annotation

Codecov / codecov/patch

expect/_snapshot_serializer.ts#L13-L15

Added lines #L13 - L15 were not covered by tests
75 changes: 75 additions & 0 deletions expect/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,78 @@ export type EqualOptions = {
export interface EqualOptionUtil extends MatcherContext {
strictCheck?: boolean;
}

export interface Colors {
comment: { close: string; open: string };
content: { close: string; open: string };
prop: { close: string; open: string };
tag: { close: string; open: string };
value: { close: string; open: string };
}
type Indent = (arg0: string) => string;
type Print = (arg0: unknown) => string;

export type Refs = Array<unknown>;

export type CompareKeys = ((a: string, b: string) => number) | null | undefined;

export interface Config {
callToJSON: boolean;
compareKeys: CompareKeys;
colors: Colors;
escapeRegex: boolean;
escapeString: boolean;
indent: string;
maxDepth: number;
maxWidth: number;
min: boolean;
plugins: SnapshotPlugin;
printBasicPrototype: boolean;
printFunctionName: boolean;
spacingInner: string;
spacingOuter: string;
}

export type Printer = (
val: unknown,
config: Config,
indentation: string,
depth: number,
refs: Refs,
hasCalledToJSON?: boolean,
) => string;

interface PluginOptions {
edgeSpacing: string;
min: boolean;
spacing: string;
}

type Test = (arg0: any) => boolean;

export interface NewSnapshotPlugin {
serialize: (
val: any,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer,
) => string;
test: Test;
}

export interface OldSnapshotPlugin {
print: (
val: unknown,
print: Print,
indent: Indent,
options: PluginOptions,
colors: Colors,
) => string;
test: Test;
}

export type SnapshotPlugin = NewSnapshotPlugin | OldSnapshotPlugin;

export type SnapshotPlugins = Array<SnapshotPlugin>;
2 changes: 2 additions & 0 deletions expect/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
toStrictEqual,
toThrow,
} from "./_matchers.ts";
import { addSerializer } from "./_snapshot_serializer.ts";
import { isPromiseLike } from "./_utils.ts";
import {
any,
Expand Down Expand Up @@ -197,6 +198,7 @@ export function expect(value: unknown, customMessage?: string): Expected {
}

expect.addEqualityTesters = addCustomEqualityTesters;
expect.addSnapshotSerializers = addSerializer;
expect.extend = setExtendMatchers;

expect.anything = anything;
Expand Down