-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
testFnWrapper.ts
131 lines (119 loc) · 4.09 KB
/
testFnWrapper.ts
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
import { isFunctionAsync } from '../utils'
import { logHookError } from './errorHandler'
import { executeHooksWithArgs, executeAsync, runSync } from '../shim'
import type {
WrapperMethods,
SpecFunction,
BeforeHookParam,
AfterHookParam,
JasmineContext
} from './types'
const STACKTRACE_FILTER = [
'node_modules/webdriver/',
'node_modules/webdriverio/',
'node_modules/@wdio/',
'(internal/process/task',
]
/**
* wraps test framework spec/hook function with WebdriverIO before/after hooks
*
* @param {string} type Test/Step or Hook
* @param {object} spec specFn and specFnArgs
* @param {object} before beforeFn and beforeFnArgs
* @param {object} after afterFn and afterFnArgs
* @param {string} cid cid
* @param {number} repeatTest number of retries if test fails
* @return {*} specFn result
*/
export const testFnWrapper = function (
this: unknown,
...args: [
string,
SpecFunction,
BeforeHookParam<unknown>,
AfterHookParam<unknown>,
string,
number
]
) {
return testFrameworkFnWrapper.call(this, { executeHooksWithArgs, executeAsync, runSync }, ...args)
}
/**
* wraps test framework spec/hook function with WebdriverIO before/after hooks
*
* @param {object} wrapFunctions executeHooksWithArgs, executeAsync, runSync
* @param {string} type Test/Step or Hook
* @param {object} spec specFn and specFnArgs array
* @param {object} before beforeFn and beforeFnArgs function
* @param {object} after afterFn and afterFnArgs function
* @param {string} cid cid
* @param {number} repeatTest number of retries if test fails
* @return {*} specFn result
*/
export const testFrameworkFnWrapper = async function (
this: unknown,
{ executeHooksWithArgs, executeAsync, runSync }: WrapperMethods,
type: string,
{ specFn, specFnArgs }: SpecFunction,
{ beforeFn, beforeFnArgs }: BeforeHookParam<unknown>,
{ afterFn, afterFnArgs }: AfterHookParam<unknown>,
cid: string,
repeatTest = 0
) {
const retries = { attempts: 0, limit: repeatTest }
const beforeArgs = beforeFnArgs(this)
await logHookError(`Before${type}`, await executeHooksWithArgs(`before${type}`, beforeFn, beforeArgs), cid)
let promise
let result
let error
/**
* user wants handle async command using promises, no need to wrap in fiber context
*/
if (isFunctionAsync(specFn) || !runSync) {
promise = executeAsync.call(this, specFn, retries, specFnArgs)
} else {
promise = new Promise(runSync.call(this, specFn, retries, specFnArgs))
}
const testStart = Date.now()
try {
result = await promise
} catch (err: any) {
if (err.stack) {
err.stack = filterStackTrace(err.stack)
}
error = err
}
const duration = Date.now() - testStart
let afterArgs = afterFnArgs(this)
/**
* ensure errors are caught in Jasmine tests too
* (in Jasmine failing assertions are not causing the test to throw as
* oppose to other common assertion libraries like chai)
*/
if (!error && afterArgs[0] && (afterArgs as [JasmineContext, unknown])[0].failedExpectations && (afterArgs as [JasmineContext, unknown])[0].failedExpectations.length) {
error = (afterArgs as [JasmineContext, unknown])[0].failedExpectations[0]
}
afterArgs.push({
retries,
error,
result,
duration,
passed: !error
})
await logHookError(`After${type}`, await executeHooksWithArgs(`after${type}`, afterFn, [...afterArgs]), cid)
if (error && !error.matcherName) {
throw error
}
return result
}
/**
* Filter out internal stacktraces. exporting to allow testing of the function
* @param {string} stack Stacktrace
* @returns {string}
*/
export const filterStackTrace = (stack: string): string => {
return stack
.split('\n')
.filter(line => !STACKTRACE_FILTER.some(l => line.includes(l)))
.join('\n')
}