-
Notifications
You must be signed in to change notification settings - Fork 1
/
tester.js
59 lines (51 loc) · 1.65 KB
/
tester.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { performance } from 'perf_hooks'
import { pathToFileURL, fileURLToPath } from 'url'
import { dirname, join } from 'path'
const colorize = (color) => (text) => `\u001b[${color}m${text}\u001b[0m`
const clear = colorize(0)
const red = colorize(31)
const green = colorize(32)
const yellow = colorize(33)
const blue = colorize(34)
const magenta = colorize(35)
const cyan = colorize(36)
const white = colorize(37)
const start = performance.now()
const rootDir = dirname(fileURLToPath(import.meta.url))
const finish = async (err, name) => {
const elapsed = performance.now() - start
console.log(yellow('\nTIME:'), `completed in ${yellow(fmtTime(elapsed))}`)
}
const ellipsis = (m) =>
m.length <= process.stdout.columns - 12
? m
: m
.slice(0, cap - 1)
.trim()
.padEnd(cap, '…')
const removeDot = (n) => (n.endsWith('.') ? ` ${n.slice(0, -1)}` : n)
const fmtTime = (elapsed) =>
elapsed > 999
? `${removeDot((elapsed / 1000).toFixed(3).slice(0, 4))}s`
: `${removeDot(elapsed.toFixed(2).slice(0, 3))}ms`
const filePath = 'tests.js'
console.log('\n -> running tests of', cyan(filePath))
const { t } = await import(pathToFileURL(join(rootDir, filePath)))
let prev = performance.now()
for (const [name, test] of Object.entries(t)) {
try {
await test()
const end = performance.now()
console.log(green('PASS:'), yellow(fmtTime(end - prev)), ellipsis(name))
prev = end
} catch (err) {
await finish()
console.log(red('FAIL:'), name)
err.response?.body
? console.error(err.message, err.response.body)
: console.error(err)
process.exit(1)
}
}
await finish()
console.log('All tests passed')