-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretry.js
245 lines (189 loc) · 6.13 KB
/
retry.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const setup = require('../index.js')
const QUnit = require('qunit')
const timeout = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
const retry = setup(QUnit.test)
QUnit.module('test retries and result message', hooks => {
hooks.after(assert => {
assert.equal(QUnit.config.current.assertions[0].message, '(Retried 5 times)', 'message shows retries')
})
retry('test retry five times', function (assert, currentRun) {
assert.equal(currentRun, 5)
}, 5)
})
QUnit.module('test retries, should stop at 3 retries', hooks => {
hooks.after(assert => {
assert.equal(QUnit.config.current.assertions[0].message, '(Retried 3 times)', 'message shows retries')
})
retry('test retry five times', function (assert, currentRun) {
assert.equal(currentRun, 3)
}, 5)
})
retry('test default: retry runs twice - initial attempt plus one retry', function (assert, currentRun) {
assert.expect(1)
assert.equal(currentRun, 2)
})
retry('test retry five times', function (assert, currentRun) {
assert.expect(1)
assert.equal(currentRun, 5)
}, 5)
retry('test retry async', async function (assert, currentRun) {
assert.expect(1)
await timeout(100)
assert.equal(currentRun, 4)
}, 4)
retry('promise reject', async function (assert, currentRun) {
if (currentRun === 2) {
await Promise.reject(new Error('should be handled'))
}
assert.equal(currentRun, 5)
}, 5)
QUnit.module('hook context', function (hooks) {
hooks.beforeEach(function () {
this.sharedValue = 'myContext'
})
QUnit.test('qunit test', function (assert) {
assert.equal(this.sharedValue, 'myContext')
})
retry('retry matches qunit test behaviour', function (assert, currentRun) {
assert.equal(this.sharedValue, 'myContext')
assert.equal(currentRun, 2)
})
retry('environment it reset on each retry', function (assert, currentRun) {
assert.equal(this.localValue, undefined)
this.localValue = 'local'
assert.equal(currentRun, 2)
})
})
QUnit.module('currentRun count', function () {
// tests are order dependent
// count retries in retryTest
// assert correct count in another test
let execCount = 0
retry('count retries', function (assert, currentRun) {
execCount = execCount + 1
assert.equal(currentRun, 5)
}, 5)
QUnit.test('execCount for retryTest', function (assert) {
assert.equal(execCount, 5)
})
})
QUnit.module('hooks count', function () {
// tests are order dependent
// count retries in retryTest
// assert correct count in another test
let execCount = 0
let beforeCount = 0
let afterCount = 0
QUnit.module('count hooks async', function (hooks) {
hooks.beforeEach(async function () {
await timeout(100)
beforeCount++
})
hooks.afterEach(async function () {
await timeout(100)
afterCount++
})
retry('count retries', function (assert, currentRun) {
execCount++
assert.equal(beforeCount, currentRun, 'beforeCount should match currentRun')
assert.equal(afterCount, currentRun - 1, 'afterCount one less than currentRun')
assert.equal(currentRun, 5)
}, 5)
})
QUnit.test('test hooks count', function (assert) {
assert.equal(execCount, 5)
assert.equal(beforeCount, 5)
assert.equal(afterCount, 5)
})
})
QUnit.module('default max runs', function () {
const calls = []
const retryThrice = setup(QUnit.test, 3)
retryThrice('count retries', function (assert, currentRun) {
calls.push(currentRun)
assert.equal(currentRun, 3)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [1, 2, 3])
})
})
QUnit.module('retry.todo', function () {
const calls = []
retry.todo('count retries', function (assert, currentRun) {
calls.push(currentRun)
assert.ok(false)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [1, 2])
})
})
QUnit.module('retry.skip', function () {
const calls = []
retry.skip('count retries', function (assert, currentRun) {
calls.push(currentRun)
assert.equal(currentRun, 2)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [])
})
})
QUnit.module('retry.if', function () {
const calls = []
retry.if('count true retries', true, function (assert, currentRun) {
calls.push([true, currentRun])
assert.equal(currentRun, 2)
})
retry.if('count false retries', false, function (assert, currentRun) {
calls.push([false, currentRun])
assert.equal(currentRun, 2)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [[true, 1], [true, 2]])
})
})
QUnit.module('retry.each', function () {
const calls = []
retry.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])
assert.equal(currentRun, 2)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [['A', 1], ['A', 2], ['B', 1], ['B', 2], ['C', 1], ['C', 2]])
})
})
QUnit.module('retry.todo.each', function () {
const calls = []
retry.todo.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])
assert.ok(false)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [['A', 1], ['A', 2], ['B', 1], ['B', 2], ['C', 1], ['C', 2]])
})
})
QUnit.module('retry.skip.each', function () {
const calls = []
retry.skip.each('count retries', ['A', 'B', 'C'], function (assert, data, currentRun) {
calls.push([data, currentRun])
assert.equal(currentRun, 2)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [])
})
})
QUnit.module('retry.if.each', function () {
const calls = []
retry.if.each('count true retries', true, ['A', 'B'], function (assert, data, currentRun) {
calls.push([true, data, currentRun])
assert.equal(currentRun, 2)
})
retry.if.each('count false retries', false, ['A', 'B'], function (assert, data, currentRun) {
calls.push([false, data, currentRun])
assert.equal(currentRun, 2)
})
QUnit.test('verify calls', function (assert) {
assert.deepEqual(calls, [[true, 'A', 1], [true, 'A', 2], [true, 'B', 1], [true, 'B', 2]])
})
})