-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
74 lines (62 loc) · 1.62 KB
/
test.mjs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import startCounter from './count-promises.js'
import assert from 'node:assert'
import { setTimeout } from 'node:timers/promises'
await 1 // clears out the Module.run promise
{
const stop = startCounter()
await 1
assert.strictEqual(stop(), 2)
}
{
const stop = startCounter({ locations: true })
await 1
assert.deepEqual(stop(), {
[`${import.meta.url}:16:3`]: 2
})
}
{
const stop = startCounter({})
await Promise.resolve()
assert.strictEqual(stop(), 2)
}
{
const stop = startCounter({ locations: true })
await Promise.resolve()
assert.deepEqual(stop(), {
[`${import.meta.url}:30:17`]: 2
})
}
{
const stop = startCounter({})
await new Promise(resolve => resolve())
assert.strictEqual(stop(), 2)
}
{
const stop = startCounter({})
await new Promise(resolve => resolve())
assert.strictEqual(stop(), 2)
}
{
setTimeout(100).then(async () => {
return Promise.resolve()
}, 100)
const stop = startCounter({ continuation: false })
await setTimeout(200)
assert.strictEqual(stop(), 6)
}
{
setTimeout(100).then(async () => {
return Promise.resolve()
}, 100)
const stop = startCounter({ continuation: true })
await setTimeout(200)
assert.strictEqual(stop(), 2)
}
{ // regression test for https://github.com/bengl/count-promises/issues/1
const stop = startCounter({ continuation: true, locations: true })
await fetch('http://example.com')
assert.ok(Object.keys(stop()).length > 10)
}
// Hello there! You might be wondering why I didn't use Node.js built-in test
// framework. The reason is that the test framework adds a bunch of promises
// everywhere, polluting these tests.