Skip to content

Commit

Permalink
initial prep for deno, still has a few issues
Browse files Browse the repository at this point in the history
	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/[email protected]/node/process"' has no exported member 'stdout'. Did you mean to use 'import stdout from "https://deno.land/[email protected]/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/[email protected]/node/process"' has no exported member 'stderr'. Did you mean to use 'import stderr from "https://deno.land/[email protected]/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

denoland/deno#7102
  • Loading branch information
balupton committed Aug 18, 2020
1 parent 468fafb commit 3f4a9ea
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'

// Import
import util from 'util'
import assert from 'assert'
import {
ok as _ok,
strictEqual as _strictEqual,
deepStrictEqual as _deepStrictEqual,
} from 'assert'
import { inspect as _inspect } 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
Expand All @@ -16,19 +21,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
)
}

Expand All @@ -47,13 +50,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 */
Expand All @@ -73,19 +72,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 _inspect(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]))
}
Expand All @@ -97,7 +97,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 = [
'------------------------------------',
Expand All @@ -116,24 +116,24 @@ 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 {
console.log(lines.join('\n'))
}
}

/** 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,
testName = 'equal assertion',
next?: Errback
): void | never {
try {
assert.strictEqual(actual, expected, testName)
_strictEqual(actual, expected, testName)
} catch (checkError) {
logComparison(actual, expected, checkError)
if (next) {
Expand All @@ -154,7 +154,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) {
Expand All @@ -175,7 +175,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) {
Expand All @@ -196,7 +196,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) {
Expand All @@ -217,7 +217,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) {
Expand All @@ -237,7 +237,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) {
Expand All @@ -257,10 +257,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)
Expand All @@ -283,7 +283,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) {
Expand All @@ -306,7 +306,7 @@ export function contains(
if (testName == null)
testName = `Expected [${actual}] to contain [${expected}]`
try {
assert.ok(actual.indexOf(expected) !== -1, testName)
_ok(actual.indexOf(expected) !== -1, testName)
} catch (checkError) {
if (next) {
next(checkError)
Expand Down

0 comments on commit 3f4a9ea

Please sign in to comment.