fix(std/testing/asserts): Support browsers (#5847)

This commit is contained in:
Nayeem Rahman 2020-05-25 18:32:34 +01:00 committed by GitHub
parent 08f74e1f6a
commit 4ebd243423
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View file

@ -314,3 +314,16 @@ export function foo(): string {
`https://deno.land/std/` is intended to be baseline functionality that all Deno `https://deno.land/std/` is intended to be baseline functionality that all Deno
programs can rely on. We want to guarantee to users that this code does not programs can rely on. We want to guarantee to users that this code does not
include potentially unreviewed third party code. include potentially unreviewed third party code.
#### Document and maintain browser compatiblity.
If a module is browser compatible, include the following in the JSDoc at the top
of the module:
```ts
/** This module is browser compatible. */
```
Maintain browser compatibility for such a module by either not using the global
`Deno` namespace or feature-testing for it. Make sure any new dependencies are
also browser compatible.

View file

@ -1,6 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/** /** A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
* A module to print ANSI terminal colors. Inspired by chalk, kleur, and colors
* on npm. * on npm.
* *
* ``` * ```
@ -10,8 +9,10 @@
* *
* This module supports `NO_COLOR` environmental variable disabling any coloring * This module supports `NO_COLOR` environmental variable disabling any coloring
* if `NO_COLOR` is set. * if `NO_COLOR` is set.
*/ *
const { noColor } = Deno; * This module is browser compatible. */
const noColor = globalThis.Deno?.noColor ?? true;
interface Code { interface Code {
open: string; open: string;

View file

@ -1,4 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/** This module is browser compatible. Do not rely on good formatting of values
* for AssertionError messages in browsers. */
import { red, green, white, gray, bold } from "../fmt/colors.ts"; import { red, green, white, gray, bold } from "../fmt/colors.ts";
import diff, { DiffType, DiffResult } from "./diff.ts"; import diff, { DiffType, DiffResult } from "./diff.ts";
@ -17,7 +20,7 @@ export class AssertionError extends Error {
} }
function format(v: unknown): string { function format(v: unknown): string {
let string = Deno.inspect(v); let string = globalThis.Deno ? Deno.inspect(v) : String(v);
if (typeof v == "string") { if (typeof v == "string") {
string = `"${string.replace(/(?=["\\])/g, "\\")}"`; string = `"${string.replace(/(?=["\\])/g, "\\")}"`;
} }
@ -254,7 +257,7 @@ export function assertStrContains(
): void { ): void {
if (!actual.includes(expected)) { if (!actual.includes(expected)) {
if (!msg) { if (!msg) {
msg = `actual: "${actual}" expected to contains: "${expected}"`; msg = `actual: "${actual}" expected to contain: "${expected}"`;
} }
throw new AssertionError(msg); throw new AssertionError(msg);
} }
@ -286,7 +289,7 @@ export function assertArrayContains(
return; return;
} }
if (!msg) { if (!msg) {
msg = `actual: "${actual}" expected to contains: "${expected}"`; msg = `actual: "${actual}" expected to contain: "${expected}"`;
msg += "\n"; msg += "\n";
msg += `missing: ${missing}`; msg += `missing: ${missing}`;
} }

View file

@ -169,7 +169,7 @@ test("testingAssertStringContainsThrow", function (): void {
} catch (e) { } catch (e) {
assert( assert(
e.message === e.message ===
`actual: "Denosaurus from Jurassic" expected to contains: "Raptor"` `actual: "Denosaurus from Jurassic" expected to contain: "Raptor"`
); );
assert(e instanceof AssertionError); assert(e instanceof AssertionError);
didThrow = true; didThrow = true;

View file

@ -1,4 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/** This module is browser compatible. */
interface FarthestPoint { interface FarthestPoint {
y: number; y: number;
id: number; id: number;