| 16 | t.timeout = 5 |
| 17 | |
| 18 | async function test(o, name, options, fn) { |
| 19 | typeof options !== 'object' && (fn = options, options = {}) |
| 20 | const line = new Error().stack.split('\n')[3].match(':([0-9]+):')[1] |
| 21 | |
| 22 | await 1 |
| 23 | |
| 24 | if (only && !o) |
| 25 | return |
| 26 | |
| 27 | tests[line] = { fn, line, name } |
| 28 | promise = promise.then(() => Promise.race([ |
| 29 | new Promise((resolve, reject) => |
| 30 | fn.timer = setTimeout(() => reject('Timed out'), (options.timeout || t.timeout) * 1000) |
| 31 | ), |
| 32 | failed |
| 33 | ? (ignored++, ignore) |
| 34 | : fn() |
| 35 | ])) |
| 36 | .then(async x => { |
| 37 | clearTimeout(fn.timer) |
| 38 | if (x === ignore) |
| 39 | return |
| 40 | |
| 41 | if (!Array.isArray(x)) |
| 42 | throw new Error('Test should return result array') |
| 43 | |
| 44 | const [expected, got] = await Promise.all(x) |
| 45 | if (expected !== got) { |
| 46 | failed = true |
| 47 | throw new Error(util.inspect(expected) + ' != ' + util.inspect(got)) |
| 48 | } |
| 49 | |
| 50 | tests[line].succeeded = true |
| 51 | process.stdout.write('✅') |
| 52 | }) |
| 53 | .catch(err => { |
| 54 | tests[line].failed = failed = true |
| 55 | tests[line].error = err instanceof Error ? err : new Error(util.inspect(err)) |
| 56 | }) |
| 57 | .then(() => { |
| 58 | ++done === Object.keys(tests).length && exit() |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | function exit() { |
| 63 | let success = true |