mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
fix(expect): support expect.addSnapshotSerializer
(#6173)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
parent
e61da9f33e
commit
e7c6d36abf
15
expect/_serializer.ts
Normal file
15
expect/_serializer.ts
Normal 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;
|
||||
}
|
23
expect/_serializer_test.ts
Normal file
23
expect/_serializer_test.ts
Normal 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);
|
||||
});
|
@ -915,3 +915,5 @@ export interface OldSnapshotPlugin {
|
||||
) => string;
|
||||
test: Test;
|
||||
}
|
||||
|
||||
export type SnapshotPlugin = NewSnapshotPlugin | OldSnapshotPlugin;
|
||||
|
@ -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;
|
||||
|
@ -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}.
|
||||
|
Loading…
Reference in New Issue
Block a user