-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
smoke.runner.js
502 lines (468 loc) · 15.4 KB
/
smoke.runner.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import { readFile, unlink, exists, rename } from 'fs'
import path from 'path'
import assert from 'assert'
import { promisify } from 'util'
import { sleep } from '../packages/wdio-utils/src/utils'
import { SevereServiceError } from '../packages/node_modules/webdriverio'
const fs = {
readFile: promisify(readFile),
unlink: promisify(unlink),
exists: promisify(exists),
rename: promisify(rename)
}
import launch from './helpers/launch'
import {
SERVICE_LOGS,
LAUNCHER_LOGS,
REPORTER_LOGS,
JASMINE_REPORTER_LOGS,
CUCUMBER_REPORTER_LOGS,
} from './helpers/fixtures'
async function runTests (tests) {
/**
* Usage example: `npm run test:smoke -- customService`
*/
const testFilter = process.argv[2]
if (process.env.CI || testFilter) {
const testsFiltered = testFilter ? tests.filter(test => test.name === testFilter) : tests
if (testsFiltered.length === 0) {
throw new Error(
`No test was selected! Smoke test "${testFilter}" ` +
`picked but only ${tests.map(test => test.name).join(', ')} available`
)
}
// sequential
for (let test of testsFiltered) {
await test()
}
} else {
// parallel
await Promise.all(tests.map(test => test().catch((err) => {
throw new Error(`Smoke test failed with name ${test.name}, ${err}`)
})))
}
}
/**
* Mocha wdio testrunner tests
*/
const mochaTestrunner = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [
path.resolve(__dirname, 'mocha', 'test.js'),
path.resolve(__dirname, 'mocha', 'test-middleware.js'),
path.resolve(__dirname, 'mocha', 'test-waitForElement.js'),
path.resolve(__dirname, 'mocha', 'test-skipped.js')
]
})
assert.strictEqual(skippedSpecs, 1)
}
/**
* Mocha wdio testrunner tests with asynchronous execution
*/
const mochaAsyncTestrunner = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'command.hook.config.js'),
{ specs: [path.resolve(__dirname, 'mocha', 'test-async.js')] }
)
assert.strictEqual(skippedSpecs, 0)
}
/**
* Jasmine wdio testrunner tests
*/
const jasmineTestrunner = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [
path.resolve(__dirname, 'jasmine', 'test.js'),
path.resolve(__dirname, 'jasmine', 'test-skipped.js')
],
framework: 'jasmine'
})
assert.strictEqual(skippedSpecs, 1)
}
/**
* Jasmine reporter
*/
const jasmineReporter = async () => {
try {
await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'jasmine', 'reporter.js')],
reporters: [['smoke-test', { foo: 'bar' }]],
framework: 'jasmine',
outputDir: __dirname + '/jasmine'
})
} catch (err) {
// expected failure
}
await sleep(100)
const reporterLogsPath = path.join(__dirname, 'jasmine', 'wdio-0-0-smoke-test-reporter.log')
const reporterLogs = await fs.readFile(reporterLogsPath)
assert.equal(reporterLogs.toString(), JASMINE_REPORTER_LOGS)
await fs.unlink(reporterLogsPath)
}
/**
* Jasmine timeout test
*/
const jasmineTimeout = async () => {
const logFile = path.join(__dirname, 'jasmineTimeout.spec.log')
const err = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'jasmine', 'test-timeout.js')],
reporters: [
['spec', {
outputDir: __dirname,
stdout: false,
logFile
}]
],
framework: 'jasmine'
}
).catch(err => err)
assert.strictEqual(err.message, 'Smoke test failed')
const specLogs = (await fs.readFile(logFile)).toString()
assert.ok(
specLogs.includes('Error: Timeout - Async function did not complete within 1000ms (custom timeout)'),
'spec was not failing due to timeout error'
)
assert.ok(
!specLogs.includes('RangeError: Maximum call stack size exceeded'),
'spec was failing due to unexpected "Maximum call stack size exceeded"'
)
}
/**
* Cucumber wdio testrunner tests
*/
const cucumberTestrunner = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'cucumber-hooks.conf.js'),
{
specs: [
path.resolve(__dirname, 'cucumber', 'test.feature'),
path.resolve(__dirname, 'cucumber', 'test-skipped.feature')
],
cucumberOpts: {
tagExpression: '(not @SKIPPED_TAG)',
ignoreUndefinedDefinitions: true,
retry: 1,
retryTagFilter: '@retry',
scenarioLevelReporter: true
}
}
)
assert.strictEqual(skippedSpecs, 1)
}
/**
* Cucumber fail due to failAmbiguousDefinitions
*/
const cucumberFailAmbiguousDefinitions = async () => {
const hasFailed = await launch(
path.resolve(__dirname, 'helpers', 'cucumber-hooks.conf.js'),
{
specs: [path.resolve(__dirname, 'cucumber', 'test.feature')],
cucumberOpts: {
ignoreUndefinedDefinitions: true,
failAmbiguousDefinitions: true,
names: ['failAmbiguousDefinitions']
}
}
).then(
() => false,
() => true
)
assert.equal(hasFailed, true)
}
/**
* Cucumber reporter
*/
const cucumberReporter = async () => {
const basePath = path.resolve(__dirname, 'cucumber', 'reporter')
try {
await launch(
path.resolve(basePath, 'reporter.config.js'),
{
specs: [path.resolve(basePath, 'reporter.feature')],
outputDir: basePath,
})
} catch (err) {
// expected failure
}
await sleep(100)
const reporterLogsPath = path.join(basePath, 'wdio-0-0-smoke-test-reporter.log')
const reporterLogs = await fs.readFile(reporterLogsPath)
assert.equal(reporterLogs.toString(), CUCUMBER_REPORTER_LOGS)
await fs.unlink(reporterLogsPath)
}
/**
* wdio test run with custom service
*/
const customService = async () => {
await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'service.js')],
services: [['smoke-test', { foo: 'bar' }]]
})
await sleep(100)
const serviceLogs = await fs.readFile(path.join(__dirname, 'helpers', 'service.log'))
assert.equal(serviceLogs.toString(), SERVICE_LOGS)
const launcherLogs = await fs.readFile(path.join(__dirname, 'helpers', 'launcher.log'))
assert.equal(launcherLogs.toString(), LAUNCHER_LOGS)
}
/**
* wdio test run with custom reporter as string
*/
const customReporterString = async () => {
await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'reporter.js')],
reporters: [['smoke-test', { foo: 'bar' }]]
})
await sleep(100)
const reporterLogsPath = path.join(__dirname, 'helpers', 'wdio-0-0-smoke-test-reporter.log')
const reporterLogs = await fs.readFile(reporterLogsPath)
assert.equal(reporterLogs.toString(), REPORTER_LOGS)
await fs.unlink(reporterLogsPath)
}
/**
* wdio test run with custom reporter as object
*/
const customReporterObject = async () => {
await launch(path.resolve(__dirname, 'helpers', 'reporter.conf.js'), {})
const reporterLogsWithReporterAsObjectPath = path.join(__dirname, 'helpers', 'wdio-0-0-CustomSmokeTestReporter-reporter.log')
const reporterLogsWithReporterAsObject = await fs.readFile(reporterLogsWithReporterAsObjectPath)
assert.equal(reporterLogsWithReporterAsObject.toString(), REPORTER_LOGS)
await fs.unlink(reporterLogsWithReporterAsObjectPath)
}
/**
* wdio test run with before/after Test/Hook
*/
const wdioHooks = async () => {
await launch(
path.resolve(__dirname, 'helpers', 'hooks.conf.js'),
{ specs: [path.resolve(__dirname, 'mocha', 'wdio_hooks.js')] })
}
/**
* multiremote wdio testrunner tests
*/
const multiremote = async () => {
await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'multiremote', 'test.js')],
capabilities: {
browserA: {
capabilities: { browserName: 'chrome' }
},
browserB: {
capabilities: { browserName: 'chrome' }
}
}
}
)
}
/**
* specfile-level retries (fail)
*/
const retryFail = async () => {
const retryFailed = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'retry_and_fail.js')],
specFileRetries: 1
}
).then(
() => false,
() => true
)
assert.equal(retryFailed, true, 'Expected retries to fail but they passed')
}
/**
* specfile-level retries (pass)
*/
const retryPass = async () => {
let retryFilename = path.join(__dirname, '.retry_succeeded')
let logfiles = ['wdio-0-0.log', 'wdio-0-1.log'].map(f => path.join(__dirname, f))
let rmfiles = [retryFilename, ...logfiles]
for (let filename of rmfiles) {
if (await fs.exists(filename)) {
fs.unlink(filename, err => {
if (err) {
throw Error(`Unable to delete ${filename}`)
}
})
}
}
await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'retry_and_pass.js')],
outputDir: path.dirname(logfiles[0]),
specFileRetries: 1,
specFileRetriesDelay: 1,
retryFilename
})
if (!await fs.exists(logfiles[0])) {
throw Error(`Expected ${logfiles[0]} to exist but it does not`)
}
if (await fs.exists(logfiles[1])) {
throw Error(`Expected ${logfiles[1]} to not exist but it does`)
}
}
/**
* wdio-shared-store-service tests
*/
const sharedStoreServiceTest = async () => {
await launch(
path.resolve(__dirname, 'helpers', 'shared-store.conf.js'),
{
specs: [
path.resolve(__dirname, 'mocha', 'shared-store-service.js'),
]
})
}
/**
* Mocha with specFiltering feature enabled
*/
const mochaSpecFiltering = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [
path.resolve(__dirname, 'mocha', 'test-empty.js'),
path.resolve(__dirname, 'mocha', 'test-skipped.js'),
path.resolve(__dirname, 'mocha', 'test-skipped-grep.js')
]
})
assert.strictEqual(skippedSpecs, 2)
}
/**
* Jasmine with specFiltering feature enabled
*/
const jasmineSpecFiltering = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [
path.resolve(__dirname, 'jasmine', 'test-empty.js'),
path.resolve(__dirname, 'jasmine', 'test-skipped.js'),
path.resolve(__dirname, 'jasmine', 'test-skipped-grep.js')
],
framework: 'jasmine'
})
assert.strictEqual(skippedSpecs, 2)
}
/**
* Mocha with spec grouping feature enabled
*/
const mochaSpecGrouping = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [
[
path.resolve(__dirname, 'mocha', 'test-empty.js'),
path.resolve(__dirname, 'mocha', 'test-middleware.js'),
path.resolve(__dirname, 'mocha', 'test-waitForElement.js'),
path.resolve(__dirname, 'mocha', 'test-skipped.js'),
path.resolve(__dirname, 'mocha', 'test-skipped-grep.js')
]
]
})
// Specs will be treated as a group, so no specs will be skipped
assert.strictEqual(skippedSpecs, 0)
}
/**
* Mocha wdio testrunner tests
*/
const standaloneTest = async () => {
const { skippedSpecs } = await launch(
path.resolve(__dirname, 'helpers', 'async.config.js'),
{
specs: [
path.resolve(__dirname, 'mocha', 'standalone.js')
]
})
assert.strictEqual(skippedSpecs, 0)
}
const severeErrorTest = async () => {
const onPrepareFailed = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'test-empty.js')],
onPrepare: () => { throw new SevereServiceError('ups') }
}
).then(() => false, () => true)
assert.equal(onPrepareFailed, true, 'Expected onPrepare to fail testrun')
const onWorkerStartFailed = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'test-empty.js')],
onWorkerStart: () => { throw new SevereServiceError('ups') }
}
).then(() => false, () => true)
assert.equal(onWorkerStartFailed, true, 'Expected onWorkerStart to fail testrun')
const onWorkerEndFailed = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'test-empty.js')],
onWorkerEnd: () => { throw new SevereServiceError('ups') }
}
).then(() => false, () => true)
assert.equal(onWorkerEndFailed, true, 'Expected onWorkerStart to fail testrun')
const onCompleteFailed = await launch(
path.resolve(__dirname, 'helpers', 'config.js'),
{
specs: [path.resolve(__dirname, 'mocha', 'test-empty.js')],
onComplete: () => { throw new SevereServiceError('ups') }
}
).then(() => false, () => true)
assert.equal(onCompleteFailed, true, 'Expected onWorkerStart to fail testrun')
}
(async () => {
const smokeTests = [
cucumberTestrunner,
cucumberFailAmbiguousDefinitions,
cucumberReporter,
standaloneTest,
mochaAsyncTestrunner,
customService,
mochaSpecFiltering,
jasmineSpecFiltering,
jasmineReporter,
jasmineTimeout,
retryFail,
retryPass,
customReporterString,
customReporterObject,
severeErrorTest
]
/**
* add smoke tests that run in sync mode and need Fibers and therefor
* aren't supported in Node.js v16 and above
*/
const [major] = process.versions.node.split('.')
if (parseInt(major) < 16) {
smokeTests.push(
mochaTestrunner,
jasmineTestrunner,
multiremote,
wdioHooks,
sharedStoreServiceTest,
mochaSpecGrouping
)
}
console.log('\nRunning smoke tests...\n')
await runTests(smokeTests)
console.log('\nAll smoke tests passed!\n')
})().catch((e) => {
// eslint-disable-next-line no-console
console.log(e.stack)
process.exit(1)
})