From 4ebd24342368adbb99582b87dc6c4b8cb6f44c87 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Mon, 25 May 2020 18:32:34 +0100 Subject: [PATCH] fix(std/testing/asserts): Support browsers (#5847) --- docs/contributing/style_guide.md | 13 +++++++++++++ std/fmt/colors.ts | 9 +++++---- std/testing/asserts.ts | 9 ++++++--- std/testing/asserts_test.ts | 2 +- std/testing/diff.ts | 2 ++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/contributing/style_guide.md b/docs/contributing/style_guide.md index ed23f31e13..0bd7c628f3 100644 --- a/docs/contributing/style_guide.md +++ b/docs/contributing/style_guide.md @@ -314,3 +314,16 @@ export function foo(): string { `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 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. diff --git a/std/fmt/colors.ts b/std/fmt/colors.ts index 6a06af20e0..a020657d99 100644 --- a/std/fmt/colors.ts +++ b/std/fmt/colors.ts @@ -1,6 +1,5 @@ // 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. * * ``` @@ -10,8 +9,10 @@ * * This module supports `NO_COLOR` environmental variable disabling any coloring * if `NO_COLOR` is set. - */ -const { noColor } = Deno; + * + * This module is browser compatible. */ + +const noColor = globalThis.Deno?.noColor ?? true; interface Code { open: string; diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index d3f8bb6784..ce72149984 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -1,4 +1,7 @@ // 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 diff, { DiffType, DiffResult } from "./diff.ts"; @@ -17,7 +20,7 @@ export class AssertionError extends Error { } function format(v: unknown): string { - let string = Deno.inspect(v); + let string = globalThis.Deno ? Deno.inspect(v) : String(v); if (typeof v == "string") { string = `"${string.replace(/(?=["\\])/g, "\\")}"`; } @@ -254,7 +257,7 @@ export function assertStrContains( ): void { if (!actual.includes(expected)) { if (!msg) { - msg = `actual: "${actual}" expected to contains: "${expected}"`; + msg = `actual: "${actual}" expected to contain: "${expected}"`; } throw new AssertionError(msg); } @@ -286,7 +289,7 @@ export function assertArrayContains( return; } if (!msg) { - msg = `actual: "${actual}" expected to contains: "${expected}"`; + msg = `actual: "${actual}" expected to contain: "${expected}"`; msg += "\n"; msg += `missing: ${missing}`; } diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 14eabca610..fb25d46cf7 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -169,7 +169,7 @@ test("testingAssertStringContainsThrow", function (): void { } catch (e) { assert( e.message === - `actual: "Denosaurus from Jurassic" expected to contains: "Raptor"` + `actual: "Denosaurus from Jurassic" expected to contain: "Raptor"` ); assert(e instanceof AssertionError); didThrow = true; diff --git a/std/testing/diff.ts b/std/testing/diff.ts index 97baa089f4..da1e827ac0 100644 --- a/std/testing/diff.ts +++ b/std/testing/diff.ts @@ -1,4 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/** This module is browser compatible. */ + interface FarthestPoint { y: number; id: number;