Skip to content

Commit

Permalink
feat(expect): support expect.addSnapshotSerialize() api (#4537)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 committed Apr 2, 2024
1 parent 6a7efa1 commit 8227afe
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
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];
}

export function getSerializer() {
return INTERNAL_PLUGINS;
}
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

0 comments on commit 8227afe

Please sign in to comment.