Skip to content

Commit

Permalink
Merge pull request #16 from nichoth/test-end
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Sep 28, 2023
2 parents 638e869 + b89d50d commit 8ea0a43
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 15 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ The implementation is <250 loc, (<500 with comments) ( https://github.com/Raynos
## Migrating from tape

```js
const test = require('tape')
const tape = require('tape')
// Tapzero exports an object with a test function property.
const test = require('tapzero').test
const tapzero = require('tapzero').test
```

```js
Expand All @@ -26,6 +26,7 @@ tapzero('my test', (t) => {
})
```

### End "automatically" if you do not call `t.plan`
```js
// tapzero "auto" ends async tests when the async function completes
tapzero('my cb test', async (t) => {
Expand All @@ -39,6 +40,17 @@ tapzero('my cb test', async (t) => {
})
```

### Plan the number of assertions
```js
tapzero('planning example', t => {
// this test will fail if we execute more or fewer
// than planned assertions
t.plan(2)
t.ok('hello')
t.equal(2, 2, 'two is two')
})
```

```js
tape('my test', (t) => {
t.equals(2, 2)
Expand Down
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Test {
constructor (name, fn, runner) {
/** @type {string} */
this.name = name
/** @type {null|number} */
this._planned = null
/** @type {null|number} */
this._actual = null
/** @type {TestFn} */
this.fn = fn
/** @type {TestRunner} */
Expand All @@ -59,6 +63,16 @@ class Test {
this.runner.report('# ' + msg)
}

/**
* Plan the number of assertions.
*
* @param {number} n
* @returns {void}
*/
plan (n) {
this._planned = n
}

/**
* @template T
* @param {T} actual
Expand Down Expand Up @@ -211,6 +225,14 @@ class Test {
)
}

if (this._planned !== null) {
this._actual = ((this._actual || 0) + 1)

if (this._actual > this._planned) {
throw new Error(`More tests than planned in TEST *${this.name}*`)
}
}

const report = this.runner.report

const prefix = pass ? 'ok' : 'not ok'
Expand Down Expand Up @@ -272,7 +294,19 @@ class Test {
if (maybeP && typeof maybeP.then === 'function') {
await maybeP
}

this.done = true

if (this._planned !== null && this._actual !== null) {
if (this._planned > this._actual) {
throw new Error(`Test ended before the planned number
planned: ${this._planned}
actual: ${this._actual}
`
)
}
}

return this._result
}
}
Expand Down
12 changes: 6 additions & 6 deletions test/unit/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const test = require('@pre-bundled/tape')
const { TestRunner } = require('../../index.js')
const { collect, trimPrefix } = require('../util.js')

test('zerotap outputs TAP', (assert) => {
test('tapzero outputs TAP', (assert) => {
const h = new TestRunner(collect(verify))
h.add('one', (t) => {
t.ok(true)
Expand All @@ -33,7 +33,7 @@ test('zerotap outputs TAP', (assert) => {
}
})

test('zerotap with two blocks', (assert) => {
test('tapzero with two blocks', (assert) => {
const h = new TestRunner(collect(verify))
h.add('one', (t) => {
t.ok(true)
Expand Down Expand Up @@ -139,7 +139,7 @@ test('zerotap handles errors', (assert) => {
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
...
1..1
Expand Down Expand Up @@ -185,7 +185,7 @@ test('zerotap handles multiple asserts', (assert) => {
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
...
1..3
Expand Down Expand Up @@ -302,7 +302,7 @@ test('zerotap undefined is string', (assert) => {
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
...
1..1
Expand Down Expand Up @@ -344,7 +344,7 @@ test('zerotap fail', (assert) => {
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
...
1..1
Expand Down
9 changes: 8 additions & 1 deletion test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ function strip (line) {
/, <anonymous>:\$LINE:\$COL\)$/, ')'
)

const lines = withoutNestedLineNumbers.split('\n')
const withoutNodeVersion = withoutNestedLineNumbers.replace(
// new RegExp('Node.js*'),
/^Node.js.*$/gm,
''
)

// const lines = withoutNestedLineNumbers.split('\n')
const lines = withoutNodeVersion.split('\n')
const newLines = lines.filter((line) => {
return !line.includes('internal/process/task_queues.js') &&
!line.includes('internal/process/next_tick.js') &&
Expand Down
2 changes: 1 addition & 1 deletion test/zora/fixtures/adding_test_cases_async_fail_out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Error: Cannot add() a test case after tests completed.
at test ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TEST/zora/fixtures/adding_test_cases_async_fail.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
4 changes: 2 additions & 2 deletions test/zora/fixtures/bailout_fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ test('will not go to the end', function _(t) {

throw new Error('Oh no!');

t.ok(true, 'will never be reached');
t.fail('should never be reached')
});

test('another one', t => {
t.ok(true, 'will never be reported');
t.fail('should never be reported');
});
2 changes: 1 addition & 1 deletion test/zora/fixtures/bailout_fail_out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Error: Oh no!
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at processTimers (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
25 changes: 25 additions & 0 deletions test/zora/fixtures/plan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const test = require('../../../index').test

test('Plan the correct number of tests', t => {
t.plan(2)
t.ok('example')
t.ok('example')
})

test('.plan with an async function', async t => {
t.plan(2)
await sleep(10)
t.ok('hello')
await sleep(20)
t.ok('hello')
})

/**
* Wait for a number of miliseconds.
* @param {number} ms
*/
async function sleep (ms) {
await new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
7 changes: 7 additions & 0 deletions test/zora/fixtures/plan_fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = require('../../../index').test

test('Plan an incorrect number of tests', t => {
t.plan(1)
t.ok('example')
t.ok('example')
})
16 changes: 16 additions & 0 deletions test/zora/fixtures/plan_fail_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TAP version 13
# Plan an incorrect number of tests
ok 1 should be truthy
$TAPE/index.js:$LINE
function rethrow () { throw err }
^

Error: More tests than planned in TEST *Plan an incorrect number of tests*
at Test._assert ($TAPE/index.js:$LINE:$COL)
at Test.ok ($TAPE/index.js:$LINE:$COL)
at Test.fn ($TEST/zora/fixtures/plan_fail.js:$LINE:$COL)
at Test.run ($TAPE/index.js:$LINE:$COL)
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
7 changes: 7 additions & 0 deletions test/zora/fixtures/plan_not_enough_fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test = require('../../../index').test

test('Execute fewer tests than planned', t => {
t.plan(3)
t.ok('example')
t.ok('example 2')
})
17 changes: 17 additions & 0 deletions test/zora/fixtures/plan_not_enough_fail_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
TAP version 13
# Execute fewer tests than planned
ok 1 should be truthy
ok 2 should be truthy
$TAPE/index.js:$LINE
function rethrow () { throw err }
^

Error: Test ended before the planned number
planned: 3
actual: 2

at Test.run ($TAPE/index.js:$LINE:$COL)
at TestRunner.run ($TAPE/index.js:$LINE:$COL)
at Timeout._onTimeout ($TAPE/index.js:$LINE:$COL)
at listOnTimeout (node:internal/timers:$LINE:$COL)
at process.processTimers (node:internal/timers:$LINE:$COL)
13 changes: 13 additions & 0 deletions test/zora/fixtures/plan_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
TAP version 13
# Plan the correct number of tests
ok 1 should be truthy
ok 2 should be truthy
# .plan with an async function
ok 3 should be truthy
ok 4 should be truthy

1..4
# tests 4
# pass 4

# ok
5 changes: 3 additions & 2 deletions test/zora/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ for (const file of JS_FILES) {
const stripped = strip(info.combined)

const expected = await readFile(
fileName.replace('.js', '_out.txt'), 'utf8'
fileName.replace('.js', '_out.txt'),
'utf8'
)
equalDiff(t, stripped, expected)
equalDiff(t, stripped.trim(), expected.trim())
})().then(t.end, t.end)
})
}
Expand Down

0 comments on commit 8ea0a43

Please sign in to comment.