-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
92 lines (79 loc) · 2.07 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict'
const runtime = require('./runtime')
const sync = require('./sync')
const network = require('./network')
const env = require('./env')
const { registerTestcaseResult } = require('./env/env')
/** @typedef {import('./runtime').RunEnv} RunEnv */
/** @typedef {import('./sync').SyncClient} SyncClient */
/**
* Takes a map of test case names and their functions, and calls the matched
* test case, or throws an error if the name is unrecognized.
*
* @param {Record<string, function(RunEnv):Promise<void>>} cases
*/
async function invokeMap (cases) {
const runenv = runtime.currentRunEnv()
if (cases[runenv.testCase]) {
try {
await invokeHelper(runenv, cases[runenv.testCase])
} catch (err) {
registerAndMessageTestcaseResult(err, runenv)
throw err
}
} else {
const err = new Error(`unrecognized test case: ${runenv.testCase}`)
registerAndMessageTestcaseResult(err, runenv)
throw err
}
}
/**
* @param {unknown} result
* @param {RunEnv} runenv
*/
function registerAndMessageTestcaseResult (result, runenv) {
runenv.recordMessage(`registerTestcaseResult: ${result}`)
registerTestcaseResult(result)
}
/**
* Runs the passed test-case and reports the result.
*
* @param {function(RunEnv):Promise<void>} fn
*/
async function invoke (fn) {
const runenv = runtime.currentRunEnv()
await invokeHelper(runenv, fn)
}
/**
* @param {RunEnv} runenv
* @param {function(RunEnv, SyncClient?):Promise<void>} fn
*/
async function invokeHelper (runenv, fn) {
let client = /** @type {SyncClient|null} */ (null)
if (fn.length >= 2) {
client = await sync.newBoundClient(runenv)
runenv.setSignalEmitter(client)
}
await runenv.recordStart()
let /** @type {unknown} */ testResult = true
try {
await fn(runenv, client)
await runenv.recordSuccess()
} catch (err) {
await runenv.recordFailure(err)
testResult = err
} finally {
if (client) {
client.close()
}
registerAndMessageTestcaseResult(testResult, runenv)
}
}
module.exports = {
invoke,
invokeMap,
env,
network,
runtime,
sync
}