diff --git a/expect/_snapshot_serializer.ts b/expect/_snapshot_serializer.ts new file mode 100644 index 000000000000..cef2f6ff3b98 --- /dev/null +++ b/expect/_snapshot_serializer.ts @@ -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; +} diff --git a/expect/_types.ts b/expect/_types.ts index 6af7997ac40f..feee63178549 100644 --- a/expect/_types.ts +++ b/expect/_types.ts @@ -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; + +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; diff --git a/expect/expect.ts b/expect/expect.ts index 87146af03850..64c1d7f64cae 100644 --- a/expect/expect.ts +++ b/expect/expect.ts @@ -50,6 +50,7 @@ import { toStrictEqual, toThrow, } from "./_matchers.ts"; +import { addSerializer } from "./_snapshot_serializer.ts"; import { isPromiseLike } from "./_utils.ts"; import { any, @@ -197,6 +198,7 @@ export function expect(value: unknown, customMessage?: string): Expected { } expect.addEqualityTesters = addCustomEqualityTesters; +expect.addSnapshotSerializers = addSerializer; expect.extend = setExtendMatchers; expect.anything = anything;