From a712b2eefe3c76a14da2033735efa49f8b2b0b21 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Wed, 19 Aug 2020 04:20:20 +1000 Subject: [PATCH] initial prep for deno, still has a few issues Check file:///Users/balupton/Projects/active/assert-helpers/edition-deno/index.ts error: TS2305 [ERROR]: Module '"https://raw.githubusercontent.com/denoland/deno/30052aa6d43378cb28dd92cd62f5f5b4e0ac4351/std/node/assert"' has no exported member 'deepStrictEqual'. deepStrictEqual as _deepStrictEqual, ~~~~~~~~~~~~~~~ at file:///Users/balupton/Projects/active/assert-helpers/edition-deno/index.ts:6:2 TS2614 [ERROR]: Module '"https://deno.land/std@0.65.0/node/process"' has no exported member 'stdout'. Did you mean to use 'import stdout from "https://deno.land/std@0.65.0/node/process"' instead? import { argv, env, stdout, stderr, versions } from 'https://deno.land/std/node/process.ts' ~~~~~~ at file:///Users/balupton/Projects/active/assert-helpers/edition-deno/index.ts:11:21 TS2614 [ERROR]: Module '"https://deno.land/std@0.65.0/node/process"' has no exported member 'stderr'. Did you mean to use 'import stderr from "https://deno.land/std@0.65.0/node/process"' instead? import { argv, env, stdout, stderr, versions } from 'https://deno.land/std/node/process.ts' ~~~~~~ at file:///Users/balupton/Projects/active/assert-helpers/edition-deno/index.ts:11:29 TS2304 [ERROR]: Cannot find name 'assert'. assert.ok(actual.indexOf(expected) !== -1, testName) ~~~~~~ at file:///Users/balupton/Projects/active/assert-helpers/edition-deno/index.ts:308:3 Found 4 errors. https://github.com/denoland/deno/issues/7101 https://github.com/denoland/deno/issues/7102 --- package.json | 2 +- source/index.ts | 61 ++++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index b4d3b7e..925c48c 100644 --- a/package.json +++ b/package.json @@ -218,4 +218,4 @@ "semi": false, "singleQuote": true } -} +} \ No newline at end of file diff --git a/source/index.ts b/source/index.ts index 8a044db..e8c77cd 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,10 +1,14 @@ 'use strict' // Import -import util from 'util' -import assert from 'assert' +import { + strictEqual as _strictEqual, + deepStrictEqual as _deepStrictEqual, +} from 'assert' +import { inspect as utilInspect } from 'util' import * as ansi from '@bevry/ansi' import Errlop from 'errlop' +import { argv, env, stdout, stderr, versions } from 'process' /** Type for a callback that receives an optional error as the first argument */ type Errback = (error?: Error) => void @@ -16,19 +20,17 @@ export function wait(delay: number, fn: (...args: any[]) => void) { /** Whether or not we are running in the node environment */ export function isNode(): boolean { - return Boolean( - typeof process !== 'undefined' && process.versions && process.versions.node - ) + return Boolean(versions && versions.node) } /** Whether or not stdout and stderr are interactive. */ export function isTTY(): boolean { return ( isNode() && - process.stdout && - process.stdout.isTTY === true && - process.stderr && - process.stderr.isTTY === true + stdout && + stdout.isTTY === true && + stderr && + stderr.isTTY === true ) } @@ -47,13 +49,9 @@ export function useColors(): boolean { // if unsupported, return false if (!isNode()) return false // if disabled, return false - if ( - process.argv.includes('--no-colors') || - process.argv.includes('--no-color') - ) - return false + if (argv.includes('--no-colors') || argv.includes('--no-color')) return false // if unspecfied, use default (tty) - return bool(process.env.COLOR) ?? bool(process.env.COLORS) ?? isTTY() + return bool(env.COLOR) ?? bool(env.COLORS) ?? isTTY() } /** Applies the color to the value if desired */ @@ -73,19 +71,20 @@ function isObject(value: any): boolean { /** * Return a stringified version of the value with indentation and colors where applicable. * Colors will be applied if the environment supports it (--no-colors not present and TTY). + * For the available options, refer to https://nodejs.org/dist/latest-v14.x/docs/api/util.html#util_util_inspect_object_options for Node.js */ -export function inspect(value: any, opts: NodeJS.InspectOptions = {}): string { +export function inspect(value: any, opts: any = {}): string { // If the terminal supports colours, and the user hasn't specified, then default to a sensible default const colors = useColors() const depth = 50 // Inspect and return using our defaults - return util.inspect(value, { colors, depth, ...opts }) + return utilInspect(value, { colors, depth, ...opts }) } /** Log the inspected values of each of the arguments to stdout */ export function log(...args: any): void { - if (isNode() && process.env.ASSERT_SILENCE) return + if (isNode() && env.ASSERT_SILENCE) return for (let i = 0; i < args.length; ++i) { console.log(inspect(args[i])) } @@ -97,7 +96,7 @@ export function logComparison( expected: any, error: Error | string | any ): void { - if (isNode() && process.env.ASSERT_SILENCE) return + if (isNode() && env.ASSERT_SILENCE) return const lines = [ '------------------------------------', @@ -116,8 +115,8 @@ export function logComparison( ) // Work for node - if (isNode() && process.stderr) { - process.stderr.write(lines.join('\n') + '\n') + if (isNode() && stderr) { + stderr.write(lines.join('\n') + '\n') } // Work for browsers else { @@ -125,7 +124,7 @@ export function logComparison( } } -/** Same as assert.strictEquals in that it performs a strict equals check, but if a failure occurs it will output detailed information */ +/** Same as assert.strictEqual in that it performs a strict equals check, but if a failure occurs it will output detailed information */ export function equal( actual: any, expected: any, @@ -133,7 +132,7 @@ export function equal( next?: Errback ): void | never { try { - assert.strictEqual(actual, expected, testName) + _strictEqual(actual, expected, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) { @@ -154,7 +153,7 @@ export function gte( next?: Errback ): void | never { try { - assert.strictEqual(actual >= expected, true, testName) + _strictEqual(actual >= expected, true, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) { @@ -175,7 +174,7 @@ export function lte( next?: Errback ): void | never { try { - assert.strictEqual(actual <= expected, true, testName) + _strictEqual(actual <= expected, true, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) { @@ -196,7 +195,7 @@ export function gt( next?: Errback ): void | never { try { - assert.strictEqual(actual > expected, true, testName) + _strictEqual(actual > expected, true, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) { @@ -217,7 +216,7 @@ export function lt( next?: Errback ): void | never { try { - assert.strictEqual(actual < expected, true, testName) + _strictEqual(actual < expected, true, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) { @@ -237,7 +236,7 @@ export function undef( next?: Errback ) { try { - assert.strictEqual(typeof actual, 'undefined', testName) + _strictEqual(typeof actual, 'undefined', testName) } catch (checkError) { logComparison(actual, 'undefined', checkError) if (next) { @@ -257,10 +256,10 @@ export function nullish( next?: Errback ) { try { - assert.strictEqual(typeof actual, 'undefined', testName) + _strictEqual(typeof actual, 'undefined', testName) } catch (e1) { try { - assert.strictEqual(actual, null, testName) + _strictEqual(actual, null, testName) } catch (e2) { const error = new Errlop(e2, e1) logComparison(actual, 'nullish', error) @@ -283,7 +282,7 @@ export function deepEqual( next?: Errback ): void | never { try { - assert.deepStrictEqual(actual, expected, testName) + _deepStrictEqual(actual, expected, testName) } catch (checkError) { logComparison(actual, expected, checkError) if (next) {