std/testing/_format.ts
Yongwook Choi 7b44ec31cd
feat(testing): Implement "assertSnapshot" (#2039)
This commits adds "assertSnapshot" API as part of "testing" module.

This API is modelled after Jest's API and can be used to compare
test results with previously saved result in a file.

Co-authored-by: Ben Heidemann <ben@heidemann.co.uk>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2022-04-20 13:46:21 +02:00

22 lines
635 B
TypeScript

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* Converts the input into a string. Objects, Sets and Maps are sorted so as to
* make tests less flaky
* @param v Value to be formatted
*/
export function format(v: unknown): string {
// deno-lint-ignore no-explicit-any
const { Deno } = globalThis as any;
return typeof Deno?.inspect === "function"
? Deno.inspect(v, {
depth: Infinity,
sorted: true,
trailingComma: true,
compact: false,
iterableLimit: Infinity,
})
: `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
}