Comparing version 6.1.1 to 7.0.0
110
assert.js
@@ -1,13 +0,11 @@ | ||
export function fail(msg) { | ||
this.log(false, 'fail', msg) | ||
} | ||
import {current} from './tst.js' | ||
export function pass(msg) { | ||
this.log(true, 'pass', msg) | ||
} | ||
export function ok(value, msg = 'should be truthy') { | ||
if (Boolean(value)) return current?.pass({operator: 'ok', message: msg}) | ||
export function ok(value, msg = 'should be truthy') { | ||
this.log(Boolean(value), 'ok', msg, { | ||
throw new Assertion({ | ||
operator: 'ok', | ||
message: msg, | ||
actual: value, | ||
expected: true | ||
expects: true | ||
}) | ||
@@ -17,5 +15,9 @@ } | ||
export function is(a, b, msg = 'should be the same') { | ||
this.log(isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b), 'is', msg, { | ||
if (isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b)) return current?.pass({operator: 'is', message: msg}) | ||
throw new Assertion({ | ||
operator: 'is', | ||
message: msg, | ||
actual: slice(a), | ||
expected: slice(b) | ||
expects: slice(b) | ||
}) | ||
@@ -25,5 +27,10 @@ } | ||
export function not(a, b, msg = 'should be different') { | ||
this.log(isPrimitive(a) || isPrimitive(b) ? !Object.is(a, b) : !deq(a, b), 'is not', msg, { | ||
if (isPrimitive(a) || isPrimitive(b) ? !Object.is(a, b) : !deq(a, b)) return current?.pass({operator: 'not', message: msg}) | ||
throw new Assertion({ | ||
operator: 'is not', | ||
message: msg, | ||
actual: slice(a), | ||
expected: new class Not { constructor(a){this.actual = a} }(a) | ||
// this contraption makes chrome debugger display nicer | ||
expects: new class Not { constructor(a){this.actual = a} }(a) | ||
}) | ||
@@ -33,5 +40,9 @@ } | ||
export function same(a, b, msg = 'should have same members') { | ||
this.log(sameMembers(a, b), 'same', msg, { | ||
if (sameMembers(a, b)) return current?.pass({operator: 'same', message: msg}) | ||
throw new Assertion({ | ||
operator: 'same', | ||
message: msg, | ||
actual: a, | ||
expected: b | ||
expects: b | ||
}) | ||
@@ -41,7 +52,10 @@ } | ||
export function any(a, list, msg = 'should be one of') { | ||
this.log(list.some(b => | ||
isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b) | ||
), 'any', msg, { | ||
if (list.some(b => isPrimitive(a) || isPrimitive(b) ? Object.is(a, b) : deq(a, b))) | ||
return current?.pass({ operator: 'any', message: msg }) | ||
throw new Assertion({ | ||
operator: 'any', | ||
message: msg, | ||
actual: slice(a), | ||
expected: new (class Any extends Array { })(...list.map(b => slice(b))) | ||
expects: new (class Any extends Array { })(...list.map(b => slice(b))) | ||
}) | ||
@@ -51,34 +65,45 @@ } | ||
export function almost (a, b, eps, msg = 'should almost equal') { | ||
this.log(isPrimitive(a) || isPrimitive(b) ? almostEqual(a, b, eps) : | ||
Array.prototype.slice.call(a).every((a0, i) => a0 === b[i] || almostEqual(a0, b[i], eps)), | ||
'almost', msg, { | ||
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}) | ||
throw new Assertion({ | ||
operator: 'almost', | ||
message: msg, | ||
actual: slice(a), | ||
expected: slice(b) | ||
expects: slice(b) | ||
}) | ||
} | ||
export function throws(fn, expected, msg = 'should throw') { | ||
export function throws(fn, expects, msg = 'should throw') { | ||
try { | ||
fn() | ||
this.log(false, 'throws', msg, { | ||
expected | ||
}) | ||
throw new Assertion({operator: 'throws', message: msg, expects}) | ||
} catch (err) { | ||
if (expected instanceof Error) { | ||
this.log(err.name === expected.name, 'throws', msg, { | ||
if (expects instanceof Error) { | ||
if (err.name === expects.name) return current?.pass({operator: 'throws', message: msg}) | ||
throw Assertion({ | ||
operator: 'throws', | ||
message: msg, | ||
actual: err.name, | ||
expected: expected.name | ||
expects: expects.name | ||
}) | ||
} else if (expected instanceof RegExp) { | ||
this.log(expected.test(err.toString()), 'throws', msg, { | ||
} else if (expects instanceof RegExp) { | ||
if (expects.test(err.toString())) return current?.pass({operator: 'throws', message: msg}) | ||
throw Assertion({ | ||
operator: 'throws', | ||
message: msg, | ||
actual: err.toString(), | ||
expected: expected | ||
expects: expects | ||
}) | ||
} else if (typeof expected === 'function') { | ||
this.log(expected(err), 'throws', msg, { | ||
} else if (typeof expects === 'function') { | ||
if (expects(err)) return current?.pass({operator: 'throws', message: msg}) | ||
throw Assertion({ | ||
operator: 'throws', | ||
message: msg, | ||
actual: err | ||
}) | ||
} else { | ||
this.log(true, 'throws', msg) | ||
} | ||
return current?.pass({operator: 'throws', message: msg}) | ||
} | ||
@@ -144,1 +169,12 @@ } | ||
const slice = a => isPrimitive(a) ? a : a.slice ? a.slice() : Object.assign({}, a) | ||
export class Assertion extends Error { | ||
constructor(opts={}) { | ||
super(opts.message); | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor); | ||
this.operator = opts.operator; | ||
this.expects = opts.expects; | ||
this.actual = opts.actual; | ||
} | ||
} | ||
Assertion.prototype.name = 'Assertion' |
{ | ||
"name": "tst", | ||
"description": "Tests without efforts", | ||
"version": "6.1.1", | ||
"version": "7.0.0", | ||
"repository": "dy/tst", | ||
"author": "Dmitry Iv.", | ||
"main": "index.js", | ||
"module": "index.js", | ||
"main": "tst.js", | ||
"module": "tst.js", | ||
"type": "module", | ||
"license": "MIT", | ||
"files": [ | ||
"index.js", | ||
"tst.js", | ||
"assert.js" | ||
@@ -14,0 +14,0 @@ ], |
# tst | ||
Tests without <em>e</em>fforts. | ||
Test without <em>e</em>fforts. | ||
## Gems | ||
* tape-like | ||
* no tooling, vanilla ESM | ||
* works with any [assert](https://www.npmjs.com/package/assert), [chai](https://www.npmjs.com/package/chai) etc. | ||
* async functions support | ||
* inspectable errors | ||
* correct stacktrace with sourcemaps | ||
* muted skipped | ||
* better colors | ||
* multiple `only` tests | ||
* start by `idle` event | ||
* ES export | ||
* `test.todo`, `test.fixme` for broken / unfinished tests | ||
* `test.node`, `test.browser` - environment conditional tests | ||
* `test.demo` - demo-run (can fail) | ||
* `console.group` in browser | ||
* better look & feel | ||
## Install | ||
## Usage | ||
[![npm install tst](https://nodei.co/npm/tst.png?mini=true)](https://npmjs.org/package/tst/) | ||
or | ||
```js | ||
import t from 'https://unpkg.com/tst?module' | ||
``` | ||
import test, {ok,is,not,throws} from 'tst.js' | ||
## Use | ||
test('these tests will all pass', () => { | ||
ok(true); | ||
ok(true, 'this time with an optional message'); | ||
ok('not true, but truthy enough'); | ||
```js | ||
t('these tests will all pass', t => { | ||
t.ok(true); | ||
t.ok(true, 'this time with an optional message'); | ||
t.ok('not true, but truthy enough'); | ||
is(1 + 1, 2); | ||
is(Math.max(1, 2, 3), 3); | ||
is({}, {}) | ||
t.is(1 + 1, 2); | ||
t.is(Math.max(1, 2, 3), 3); | ||
t.is({}, {}) | ||
t.throws(() => { | ||
throws(() => { | ||
throw new Error('oh no!'); | ||
}, /oh no!/); | ||
}) | ||
t.pass('ok') | ||
test('these tests will not pass', () => { | ||
is(42, '42'); | ||
is({}, {x:1}); | ||
}) | ||
t('these tests will not pass', t => { | ||
t.is(42, '42'); | ||
t.is({}, {x:1}); | ||
test.skip('this test will not run', () => { | ||
t.fail('nok') | ||
}) | ||
t.skip('this test will not run', t => { | ||
t.pass('ok') | ||
test.browser('browser-only test', () => { | ||
}) | ||
@@ -66,19 +51,28 @@ ``` | ||
## test types | ||
## Assertions | ||
* `test.skip` − bypass test, mutes output | ||
* `test.only` − run only the indicated test, can be multiple | ||
* `test.todo` − bypass test, indicate WIP sign | ||
* `test.node` − run test in node/deno only env. | ||
* `test.browser` − run test in browser only test. | ||
<!-- * `test.demo` − demo run, ignores doesn't count. --> | ||
* `t.ok(a, b, msg?)` − generic truthfulness assert | ||
* `t.is(a, b, msg?)` − assert with `equal` for primitives and `deepEqual` for objects | ||
* `t.not(a, b, msg?)` - assert with `equal` for primitives and `deepEqual` for objects | ||
* `t.any(a, [a, b, c], msg?)` − assert with optional results | ||
* `t.almost(a, b, eps, msg?)` − assert approximate value/array | ||
* `t.same(listA, listB, msg?)` − assert same members of a list/set/map/object | ||
* `t.throws(fn, msg?)` − fn must throw | ||
* `t.pass(msg)`, `t.fail(msf)` − pass or fail the whole test. | ||
## assertions | ||
* `ok(a, msg?)` − generic truthfulness assert | ||
* `is(a, b, msg?)` − assert with `equal` for primitives and `deepEqual` for objects | ||
* `not(a, b, msg?)` - assert with `equal` for primitives and `deepEqual` for objects | ||
* `any(a, [a, b, c], msg?)` − assert with optional results | ||
* `almost(a, b, eps, msg?)` − assert approximate value/array | ||
* `same(listA, listB, msg?)` − assert same members of a list/set/map/object | ||
* `throws(fn, msg?)` − fn must throw | ||
* `pass(msg)`, `fail(msf)` − pass or fail the whole test. | ||
### Neighbors | ||
* [uvu](https://github.com/lukeed/uvu) | ||
* [tape-modern](https://ghub.io/tape-modern) | ||
* [@goto-bus-stop/tape-modern](https://github.com/goto-bus-stop/tape-modern#readme) | ||
<p align="right">🕉️</p> | ||
<p align="center">🕉️</p> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14483
283
78
1