-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (102 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
113 lines (102 loc) · 2.89 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// @ts-check
/**
* Function that defines the value of a TestVar
* @template {any} T
* @typedef {() => T} TestVarGetter
*/
/**
* TestVar instance returned by "def()"
* @template {any} T
* @typedef {TestVarGetter<T> & { def: (f: TestVarGetter<T>) => void}} TestVar
*/
export class TestVarsError extends Error {}
/**
* Used to create TestVars for a given top level test block
*/
export class TestVarsContext {
/** @type {boolean} */
_ready
/** @type {{ [name: string]: TestVarGetter<any> }} */
_funcs
/** @type {{ [name: string]: TestVarGetter<any> }} */
_initialFuncs
/** @type {{ [name: string]: any }} results */
_results
constructor() {
this._ready = false
this._funcs = {}
this._initialFuncs = {}
this._results = {}
}
/**
* reset test block back to it's original state
* (must be called in a `beforeEach` hook in a top level describe block)
*/
setup() {
this._funcs = { ...this._initialFuncs }
this._results = {}
this._ready = true
}
/**
* optional teardown function to call after tests are completed that
* clears state and explicitly declares it as not ready
*/
teardown() {
this._funcs = {}
this._results = {}
this._ready = false
}
/**
* @template {any} T
* @param {string} name name of the TestVar
* @param {TestVarGetter<T>} f function that returns the TestVar value
* @returns {TestVar<T>}
*/
def(name, f) {
if (name in this._initialFuncs)
throw new TestVarsError(`Duplicate initialization of "${name}"`)
this._initialFuncs[name] = () => f()
const r = /** @type {TestVarGetter<T>} */ (
() => {
if (!this._ready)
throw new TestVarsError(
`"setup()" must be called before reading "${name}"`
)
if (!this._results[name]) this._results[name] = this._funcs[name]()
return /** @type {T | Promise<T>} */ (this._results[name])
}
)
return Object.assign(r, {
/** @type {(f: TestVarGetter<T>) => void} */
def: (f) => {
if (name === 'subject') {
throw new TestVarsError('Cannot redefine a test subject')
}
if (!this._ready)
throw new TestVarsError(
`"setup()" must be called before redefining "${name}"`
)
delete this._results[name]
this._funcs[name] = () => f()
}
})
}
/**
* @template {any} T
* @param {TestVarGetter<T>} f function that returns the TestVar value
* @returns {TestVar<T>}
*/
subject(f) {
return this.def('subject', f)
}
}
/** @returns {Pick<TestVarsContext, 'def' | 'setup' | 'subject' | 'teardown'>} */
export function createTestVars() {
const context = new TestVarsContext()
return {
def: (...args) => context.def(...args),
setup: (...args) => context.setup(...args),
subject: (...args) => context.subject(...args),
teardown: (...args) => context.teardown(...args)
}
}