| 1 | 'use strict' |
| 2 | |
| 3 | const assert = require('node:assert') |
| 4 | |
| 5 | function tspl (t, opts = {}) { |
| 6 | if (t) { |
| 7 | t.after(autoEnd) |
| 8 | } |
| 9 | |
| 10 | let ended = false |
| 11 | const { plan } = opts |
| 12 | let actual = 0 |
| 13 | |
| 14 | let _resolve |
| 15 | const completed = new Promise((resolve) => { |
| 16 | _resolve = resolve |
| 17 | }) |
| 18 | |
| 19 | async function autoEnd () { |
| 20 | if (ended) { |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | if (plan) { |
| 25 | assert.strictEqual(actual, plan, 'The plan was not completed') |
| 26 | } else { |
| 27 | assert.fail('The plan was not completed') |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | function end () { |
| 32 | if (ended) { |
| 33 | return |
| 34 | } |
| 35 | ended = true |
| 36 | |
| 37 | if (plan) { |
| 38 | assert.strictEqual(actual, plan, 'The plan was not completed') |
| 39 | _resolve() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const res = { |
| 44 | completed, |
| 45 | end |
| 46 | } |
| 47 | |
| 48 | for (const method of Object.keys(assert)) { |
| 49 | if (method.match(/^[a-z]/)) { |
| 50 | res[method] = (...args) => { |
| 51 | actual++ |
| 52 | const res = assert[method](...args) |
| 53 | |
| 54 | if (actual === plan) { |
| 55 | _resolve() |
| 56 | } |
| 57 | |
| 58 | return res |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return res |
| 64 | } |
| 65 | |
| 66 | module.exports = tspl |
| 67 | module.exports.default = tspl |
| 68 | module.exports.tspl = tspl |