Comparing version 2.1.0 to 3.0.0
import fastDeepEqual from 'fast-deep-equal' | ||
import { log } from './index.js' | ||
// TODO: same (members) | ||
// TODO: almost | ||
@@ -5,0 +7,0 @@ export function fail (msg) { |
89
index.js
@@ -30,2 +30,3 @@ import * as assert from './assert.js' | ||
export default function test (name, fn) { | ||
if (!fn) return test.todo(name) | ||
tests.push({ name, fn, skip: false, only: false, shouldRun: false }) | ||
@@ -35,14 +36,16 @@ start() | ||
Object.assign(test, { | ||
skip (name, fn) { | ||
tests.push({ name, fn, skip: true, only: false, shouldRun: null }) | ||
start() | ||
}, | ||
test.todo = function (name, fn) { | ||
tests.push({ name, fn, skip: true, todo: true, only: false, shouldRun: null }) | ||
} | ||
only (name, fn) { | ||
tests.push({ name, fn, skip: false, only: true, shouldRun: null }) | ||
start() | ||
} | ||
}) | ||
test.skip = function (name, fn) { | ||
tests.push({ name, fn, skip: true, only: false, shouldRun: null }) | ||
start() | ||
} | ||
test.only = function (name, fn) { | ||
tests.push({ name, fn, skip: false, only: true, shouldRun: null }) | ||
start() | ||
} | ||
let testIndex = 0 | ||
@@ -59,53 +62,10 @@ let assertIndex = 0 | ||
export function log (ok, operator, msg, info = {}) { | ||
assertIndex += 1 | ||
if (ok) { | ||
console.log(`ok ${assertIndex} — ${msg}`) | ||
console.log(`%c ✔ ${assertIndex} — ${msg}`, 'color: #229944') | ||
passed += 1 | ||
} else { | ||
console.log(`not ok ${assertIndex} — ${msg}`) | ||
failed += 1 | ||
console.log(' ---') | ||
console.log(` operator: ${operator}`) | ||
if ('expected' in info) { | ||
console.log(` expected:`, info.expected) | ||
} | ||
if ('actual' in info) { | ||
console.log(` actual:`, info.actual) | ||
} | ||
let {actual, expected, ...rest} = info | ||
for (let prop in rest) { | ||
console.log(` ${prop}:`, rest[prop]) | ||
} | ||
// find where the error occurred, and try to clean it up | ||
let err = new Error() | ||
Error.captureStackTrace(err, log); | ||
let lines = err.stack.split('\n').slice(3) | ||
let cwd = '' | ||
if (isNode) { | ||
cwd = process.cwd() | ||
if (/[/\\]/.test(cwd[0])) cwd += cwd[0] | ||
const dirname = typeof __dirname === 'string' && __dirname.replace(/dist$/, '') | ||
for (let i = 0; i < lines.length; i += 1) { | ||
if (lines[i].indexOf(dirname) !== -1) { | ||
lines = lines.slice(0, i) | ||
break | ||
} | ||
} | ||
} | ||
const stack = lines | ||
.map((line) => ` ${line.replace(cwd, '').trim()}`) | ||
.join('\n') | ||
console.log(` stack: \n${stack}`) | ||
console.log(` ...`) | ||
console.assert(false, `${assertIndex} — ${msg}`, info, (new Error())) | ||
} | ||
@@ -119,7 +79,9 @@ } | ||
if (!test.shouldRun) { | ||
if (test.skip) { | ||
// Useless info | ||
// console.log(`# skip ${test.name}`) | ||
if (test.todo) { | ||
console.log(`# todo ${test.name}`) | ||
} | ||
skipped += 1 | ||
else if (test.skip) { | ||
console.log(`%c# skip ${test.name}`, 'color: #ddd') | ||
skipped += 1 | ||
} | ||
dequeue() | ||
@@ -129,10 +91,13 @@ return | ||
console.log(`# ${test.name}`) | ||
try { | ||
let from = new Date() | ||
await test.fn(assert) | ||
let time = new Date() - from | ||
console.log(`# ${test.name} (${time}ms)`) | ||
} catch (err) { | ||
console.log(`# ${test.name}`) | ||
failed += 1 | ||
console.log(`not ok ${assertIndex} — ${err.message}`) | ||
console.error(` ${err.stack.replace(/^\s+/gm, ' ')}`) | ||
// FIXME: this syntax is due to chrome not always able to grasp the stack trace from source maps | ||
console.error(err.stack) | ||
} | ||
@@ -139,0 +104,0 @@ |
{ | ||
"name": "tst", | ||
"description": "Testing tool", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"repository": "dy/tst", | ||
@@ -6,0 +6,0 @@ "author": "Dmitry Yv", |
# tst | ||
* tape-compatible | ||
* no `t.end`, `t.plan` | ||
* async | ||
* inspectable logs | ||
* relevant stacktrace | ||
* `deepEqual` <!-- almost, same --> | ||
* muted skipped | ||
## Usage | ||
```js | ||
@@ -11,6 +21,7 @@ import t from 'https://unpkg.com/tst?module' | ||
t.eq(1 + 1, 2); | ||
t.eq(Math.max(1, 2, 3), 3); | ||
t.equal(1 + 1, 2); | ||
t.equal(Math.max(1, 2, 3), 3); | ||
t.deepEqual({}, {}) | ||
t.err(() => { | ||
t.throws(() => { | ||
throw new Error('oh no!'); | ||
@@ -23,4 +34,4 @@ }, /oh no!/); | ||
t('these tests will not pass', t => { | ||
t.eq(42, '42'); | ||
t.eq({}, {}); | ||
t.equal(42, '42'); | ||
t.equal({}, {}); | ||
@@ -34,1 +45,6 @@ t.fail('nok') | ||
``` | ||
### Neighbor art | ||
* tape-modern | ||
* @goto-bus-stop/tape-modern |
48
8158
194