Comparing version 7.2.0 to 7.3.0
@@ -1,5 +0,5 @@ | ||
import {current} from './tst.js' | ||
import { current } from './tst.js' | ||
export function ok(value, msg = 'should be truthy') { | ||
if (Boolean(value)) return current?.pass({operator: 'ok', message: msg}) | ||
if (Boolean(value)) return current?.pass({ operator: 'ok', message: msg }) | ||
@@ -10,3 +10,3 @@ throw new Assertion({ | ||
actual: value, | ||
expects: true | ||
expect: true | ||
}) | ||
@@ -16,3 +16,3 @@ } | ||
export function is(a, b, msg = 'should be the same') { | ||
if (isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b)) return current?.pass({operator: 'is', message: msg}) | ||
if (isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b)) return current?.pass({ operator: 'is', message: msg }) | ||
@@ -23,3 +23,3 @@ throw new Assertion({ | ||
actual: slice(a), | ||
expects: slice(b) | ||
expect: slice(b) | ||
}) | ||
@@ -29,3 +29,3 @@ } | ||
export function not(a, b, msg = 'should be different') { | ||
if (isPrimitive(a) || isPrimitive(b) ? !Object.is(a, b) : !deq(a, b)) return current?.pass({operator: 'not', message: msg}) | ||
if (isPrimitive(a) || isPrimitive(b) ? !Object.is(a, b) : !deq(a, b)) return current?.pass({ operator: 'not', message: msg }) | ||
@@ -37,3 +37,3 @@ throw new Assertion({ | ||
// this contraption makes chrome debugger display nicer | ||
expects: new class Not { constructor(a){this.actual = a} }(a) | ||
expect: new class Not { constructor(a) { this.actual = a } }(a) | ||
}) | ||
@@ -43,3 +43,3 @@ } | ||
export function same(a, b, msg = 'should have same members') { | ||
if (sameMembers(a, b)) return current?.pass({operator: 'same', message: msg}) | ||
if (sameMembers(a, b)) return current?.pass({ operator: 'same', message: msg }) | ||
@@ -50,3 +50,3 @@ throw new Assertion({ | ||
actual: a, | ||
expects: b | ||
expect: b | ||
}) | ||
@@ -63,11 +63,11 @@ } | ||
actual: slice(a), | ||
expects: new (class Any extends Array { })(...list.map(b => slice(b))) | ||
expect: new (class Any extends Array { })(...list.map(b => slice(b))) | ||
}) | ||
} | ||
export function almost (a, b, eps = 1.19209290e-7, msg = 'should almost equal') { | ||
export function almost(a, b, eps = 1.19209290e-7, msg = 'should almost equal') { | ||
if ( | ||
isPrimitive(a) || isPrimitive(b) ? almostEqual(a, b, eps) : | ||
[...a].every((a0, i) => a0 === b[i] || almostEqual(a0, b[i], eps)) | ||
) return current?.pass({operator: 'almost', message: msg}) | ||
[...a].every((a0, i) => a0 === b[i] || almostEqual(a0, b[i], eps)) | ||
) return current?.pass({ operator: 'almost', message: msg }) | ||
@@ -78,15 +78,15 @@ throw new Assertion({ | ||
actual: slice(a), | ||
expects: slice(b) | ||
expect: slice(b) | ||
}) | ||
} | ||
export function throws(fn, expects, msg = 'should throw') { | ||
export function throws(fn, expect, msg = 'should throw') { | ||
try { | ||
fn() | ||
throw new Assertion({operator: 'throws', message: msg, expects}) | ||
throw new Assertion({ operator: 'throws', message: msg, expect }) | ||
} catch (err) { | ||
if (err instanceof Assertion) throw err; | ||
if (expects instanceof Error) { | ||
if (err.name === expects.name) return current?.pass({operator: 'throws', message: msg}) | ||
if (expect instanceof Error) { | ||
if (err.name === expect.name) return current?.pass({ operator: 'throws', message: msg }) | ||
throw new Assertion({ | ||
@@ -96,6 +96,6 @@ operator: 'throws', | ||
actual: err.name, | ||
expects: expects.name | ||
expect: expect.name | ||
}) | ||
} else if (expects instanceof RegExp) { | ||
if (expects.test(err.toString())) return current?.pass({operator: 'throws', message: msg}) | ||
} else if (expect instanceof RegExp) { | ||
if (expect.test(err.toString())) return current?.pass({ operator: 'throws', message: msg }) | ||
throw new Assertion({ | ||
@@ -105,6 +105,6 @@ operator: 'throws', | ||
actual: err.toString(), | ||
expects: expects | ||
expect: expect | ||
}) | ||
} else if (typeof expects === 'function') { | ||
if (expects(err)) return current?.pass({operator: 'throws', message: msg}) | ||
} else if (typeof expect === 'function') { | ||
if (expect(err)) return current?.pass({ operator: 'throws', message: msg }) | ||
throw new Assertion({ | ||
@@ -117,7 +117,7 @@ operator: 'throws', | ||
return current?.pass({operator: 'throws', message: msg}) | ||
return current?.pass({ operator: 'throws', message: msg }) | ||
} | ||
} | ||
function deq (a, b) { | ||
function deq(a, b) { | ||
if (a === b) return true | ||
@@ -181,7 +181,7 @@ if (a && b) { | ||
export class Assertion extends Error { | ||
constructor(opts={}) { | ||
constructor(opts = {}) { | ||
super(opts.message); | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor); | ||
this.operator = opts.operator; | ||
this.expects = opts.expects; | ||
this.expect = opts.expect; | ||
this.actual = opts.actual; | ||
@@ -188,0 +188,0 @@ } |
{ | ||
"name": "tst", | ||
"description": "Tests without efforts", | ||
"version": "7.2.0", | ||
"version": "7.3.0", | ||
"repository": "dy/tst", | ||
@@ -6,0 +6,0 @@ "author": "Dmitry Iv.", |
@@ -88,3 +88,3 @@ const GREEN = '\u001b[32m', RED = '\u001b[31m', YELLOW = '\u001b[33m', RESET = '\u001b[0m', CYAN = '\u001b[36m', GRAY = '\u001b[30m' | ||
console.info(`actual:${RESET}`, typeof info.actual === 'string' ? JSON.stringify(info.actual) : info.actual, RED), | ||
console.info(`expects:${RESET}`, typeof (info.expects ?? info.expected) === 'string' ? JSON.stringify(info.expects ?? info.expected) : (info.expects ?? info.expected), RED), | ||
console.info(`expect:${RESET}`, typeof (info.expect ?? info.expected) === 'string' ? JSON.stringify(info.expect ?? info.expected) : (info.expect ?? info.expected), RED), | ||
console.error(new Error, RESET) | ||
@@ -91,0 +91,0 @@ ) |
13214