fix(expect): support expect.addSnapshotSerializer (#6173)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
eryue0220 2024-11-06 11:51:50 +08:00 committed by GitHub
parent e61da9f33e
commit e7c6d36abf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 61 additions and 3 deletions

15
expect/_serializer.ts Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import type { SnapshotPlugin } from "./_types.ts";
const INTERNAL_PLUGINS: SnapshotPlugin[] = [
// TODO(eryue0220): support internal snapshot serializer plugins
];
export function addSerializer(plugin: SnapshotPlugin) {
INTERNAL_PLUGINS.unshift(plugin);
}
export function getSerializer() {
return INTERNAL_PLUGINS;
}

View File

@ -0,0 +1,23 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { expect } from "./expect.ts";
import { addSerializer, getSerializer } from "./_serializer.ts";
import type { SnapshotPlugin } from "./_types.ts";
const foo: SnapshotPlugin = {
serialize() {
return "foot";
},
test() {
return false;
},
};
Deno.test("initial serializer", () => {
expect(getSerializer()).toHaveLength(0);
});
Deno.test("add serializer", () => {
addSerializer(foo);
expect(getSerializer()).toHaveLength(1);
});

View File

@ -915,3 +915,5 @@ export interface OldSnapshotPlugin {
) => string;
test: Test;
}
export type SnapshotPlugin = NewSnapshotPlugin | OldSnapshotPlugin;

View File

@ -59,9 +59,10 @@ import {
toStrictEqual,
toThrow,
} from "./_matchers.ts";
import { addSerializer } from "./_serializer.ts";
import { isPromiseLike } from "./_utils.ts";
import * as asymmetricMatchers from "./_asymmetric_matchers.ts";
import type { Tester } from "./_types.ts";
import type { SnapshotPlugin, Tester } from "./_types.ts";
export type { AnyConstructor, Async, Expected } from "./_types.ts";
@ -599,3 +600,21 @@ expect.not = {
stringContaining: asymmetricMatchers.stringNotContaining,
stringMatching: asymmetricMatchers.stringNotMatching,
};
/**
* `expect.addSnapshotSerializer` adds a module that formats application-specific data structures.
*
* For an individual test file, an added module precedes any modules from snapshotSerializers configuration,
* which precede the default snapshot serializers for built-in JavaScript types.
* The last module added is the first module tested.
*
* @example
* ```ts no-eval
* import { expect } from "@std/expect";
* import serializerAnsi from "npm:jest-snapshot-serializer-ansi";
*
* expect.addSnapshotSerializer(serializerAnsi);
* ```
*/
expect.addSnapshotSerializer = addSerializer as (
plugin: SnapshotPlugin,
) => void;

View File

@ -63,6 +63,7 @@
* - {@linkcode expect.stringMatching}
* - {@linkcode expect.not.stringMatching}
* - Utilities:
* - {@linkcode expect.addSnapshotSerializer}
* - {@linkcode expect.assertions}
* - {@linkcode expect.addEqualityTester}
* - {@linkcode expect.extend}
@ -74,8 +75,6 @@
* - `toMatchInlineSnapshot`
* - `toThrowErrorMatchingSnapshot`
* - `toThrowErrorMatchingInlineSnapshot`
* - Utilities:
* - `expect.addSnapshotSerializer`
*
* The tracking issue to add support for unsupported parts of the API is
* {@link https://github.com/denoland/std/issues/3964}.