1
1
const logger = require ( './debug' ) ;
2
2
3
- const ALLURE_AFTER_HOOK = '__allure_after_hook__' ;
4
- const ALLURE_AFTER_EACH_HOOK = '__allure_after_each_hook__' ;
5
-
6
- const isAllureAfterHook = ( nameOrFn ) => nameOrFn . includes ( ALLURE_AFTER_HOOK ) ;
3
+ const ALLURE_HOOK_NAME = {
4
+ after : '__allure_after_hook__' ,
5
+ afterEach : '__allure_after_each_hook__' ,
6
+ before : '__allure_before_hook__' ,
7
+ beforeEach : '__allure_before_each_hook__'
8
+ } ;
7
9
8
- const isAllureAfterEachHook = ( nameOrFn ) =>
9
- nameOrFn . includes ( ALLURE_AFTER_EACH_HOOK ) ;
10
+ /**
11
+ * isAllureSpecificHook
12
+ * @param {string } hookName
13
+ * @param {Array<String> } type - can be after/afterEach/before/beforeEach
14
+ * @return true/false
15
+ */
16
+ const isAllureSpecificHook = ( hookName , ...type ) => {
17
+ if ( ! hookName ) {
18
+ return false ;
19
+ }
20
+ let keys = type . flat ( Infinity ) ;
21
+ if ( keys . length === 0 ) {
22
+ keys = Object . keys ( ALLURE_HOOK_NAME ) ;
23
+ }
24
+ keys = keys . filter ( ( key ) => key in ALLURE_HOOK_NAME ) ;
25
+ return keys . some ( ( key ) => hookName . includes ( ALLURE_HOOK_NAME [ key ] ) ) ;
26
+ } ;
10
27
11
- const isAllureHook = ( nameOrFn ) =>
12
- isAllureAfterHook ( nameOrFn ) || isAllureAfterEachHook ( nameOrFn ) ;
28
+ const isAllureHook = ( hookName ) => isAllureSpecificHook ( hookName ) ;
13
29
14
30
const reorderAllureHooksArray = ( hooksArray ) => {
15
31
let commonHooks = hooksArray . filter ( ( hook ) => ! isAllureHook ( hook . title ) ) ;
16
- let allureHooks = hooksArray . filter ( ( hook ) => isAllureHook ( hook . title ) ) ;
17
- return [ ...commonHooks , ...allureHooks ] ;
32
+ let allureBeforeHooks = hooksArray . filter ( ( hook ) =>
33
+ isAllureSpecificHook ( hook . title , 'before' , 'beforeEach' )
34
+ ) ;
35
+ let allureAfterHooks = hooksArray . filter ( ( hook ) =>
36
+ isAllureSpecificHook ( hook . title , 'after' , 'afterEach' )
37
+ ) ;
38
+ return [ ...allureBeforeHooks , ...commonHooks , ...allureAfterHooks ] ;
18
39
} ;
19
40
20
41
const reorderAllureHooksGeneric = (
@@ -41,11 +62,10 @@ const reorderAllureHooksGeneric = (
41
62
}
42
63
} ;
43
64
44
- const clearAllureHookSteps = ( test , allureLogHooks = false ) => {
65
+ const clearTestAllureHookSteps = ( test , allureLogHooks = false ) => {
45
66
if ( ! ( test && test . steps && test . steps . length ) ) {
46
67
return test ;
47
68
}
48
-
49
69
test . steps = test . steps . reduce ( ( steps , step ) => {
50
70
if (
51
71
isAllureHook ( step . name ) &&
@@ -68,26 +88,31 @@ const clearAllureHookSteps = (test, allureLogHooks = false) => {
68
88
const clearAllureHookStepsFromTests = ( tests , allureLogHooks = false ) => {
69
89
if ( tests ) {
70
90
tests . forEach ( ( test ) => {
71
- clearAllureHookSteps ( test , allureLogHooks ) ;
91
+ clearTestAllureHookSteps ( test , allureLogHooks ) ;
72
92
} ) ;
73
93
}
74
94
} ;
75
95
76
- const reorderAllureAfterHooks = ( suite ) => {
96
+ const reorderAllureAfterAllHooks = ( suite ) => {
77
97
reorderAllureHooksGeneric ( suite , 'afterAll' ) ;
78
98
} ;
79
99
80
100
const reorderAllureAfterEachHooks = ( suite ) => {
81
101
reorderAllureHooksGeneric ( suite , 'afterEach' ) ;
82
102
} ;
83
103
104
+ const reorderAllureBeforeAllHooks = ( suite ) => {
105
+ reorderAllureHooksGeneric ( suite , 'beforeAll' ) ;
106
+ } ;
107
+
84
108
const reorderAllureGlobalHooks = ( suite ) => {
85
109
reorderAllureHooksGeneric ( suite , 'hooks' , '' ) ;
86
110
} ;
87
111
88
112
const reorderAllureHooks = ( suite ) => {
113
+ reorderAllureBeforeAllHooks ( suite ) ;
89
114
reorderAllureAfterEachHooks ( suite ) ;
90
- reorderAllureAfterHooks ( suite ) ;
115
+ reorderAllureAfterAllHooks ( suite ) ;
91
116
reorderAllureGlobalHooks ( suite ) ;
92
117
} ;
93
118
@@ -110,11 +135,12 @@ const isLastRootHook = () => {
110
135
return lastHook && lastHook . hookId === currentRunnable . hookId ;
111
136
} ;
112
137
113
- const setTestIdToScreenshot = ( allureEnabled , mochaIdToAllure , details ) => {
114
- if ( allureEnabled ) {
138
+ const setTestIdToScreenshot = ( { allureEnabled, allure , details } ) => {
139
+ if ( allureEnabled && allure && allure . reporter ) {
115
140
let currentTest = cy . state ( 'test' ) ;
141
+ let { mochaIdToAllure } = allure . reporter ;
116
142
if ( details && currentTest && mochaIdToAllure ) {
117
- const testIds = mochaIdToAllure [ currentTest && currentTest . id ] ;
143
+ const testIds = mochaIdToAllure [ currentTest . id ] ;
118
144
if ( testIds ) {
119
145
details [ 'testId' ] = currentTest . id ;
120
146
let testId = testIds . at ( - 1 ) ;
@@ -127,16 +153,84 @@ const setTestIdToScreenshot = (allureEnabled, mochaIdToAllure, details) => {
127
153
}
128
154
} ;
129
155
156
+ const buildPendingResults = ( { currentSpec, allure } ) => {
157
+ return {
158
+ currentSpec : currentSpec ,
159
+ allureMapping : allure . reporter . mochaIdToAllure ,
160
+ allureResults : allure . reporter . runtime . config ,
161
+ allureResultContainer : allure . reporter . suites . map (
162
+ ( suite ) => suite . testResultContainer
163
+ )
164
+ } ;
165
+ } ;
166
+
167
+ const clearAllureHookStepsFromHook = ( {
168
+ hook,
169
+ reporter,
170
+ allureLogHooks = false
171
+ } ) => {
172
+ if ( ! hook || ! reporter ) {
173
+ return ;
174
+ }
175
+ let { currentHook } = reporter ;
176
+ if ( currentHook && ! allureLogHooks && isAllureHook ( hook . title ) ) {
177
+ // remove allure hook steps so hook will not be kept in the report
178
+ currentHook . info && ( currentHook . info [ 'steps' ] = [ ] ) ;
179
+ currentHook . stepResult && ( currentHook . stepResult [ 'steps' ] = [ ] ) ;
180
+ }
181
+ } ;
182
+
183
+ const loadPendingResults = ( {
184
+ currentSpec,
185
+ allureResults,
186
+ allureMapping,
187
+ allureResultContainer
188
+ } ) => {
189
+ if ( ! currentSpec ) {
190
+ return ;
191
+ }
192
+ let currentRunningSpec = Cypress . spec . relative || Cypress . spec . absolute ;
193
+ if ( currentRunningSpec === currentSpec ) {
194
+ if (
195
+ allureMapping &&
196
+ Object . keys ( Cypress . Allure . reporter . mochaIdToAllure || { } )
197
+ . length === 0
198
+ ) {
199
+ Cypress . Allure . reporter . mochaIdToAllure = allureMapping ;
200
+ }
201
+ if ( allureResults && allureResults . writer ) {
202
+ Object . keys ( allureResults . writer ) . forEach ( ( key ) => {
203
+ Cypress . Allure . reporter . runtime . config . writer [ key ] =
204
+ allureResults . writer [ key ] ;
205
+ } ) ;
206
+ }
207
+ if ( allureResultContainer ) {
208
+ Cypress . Allure . reporter . runtime . config . writer [ 'groups' ] . push (
209
+ ...allureResultContainer
210
+ ) ;
211
+ }
212
+ } else {
213
+ cy . task (
214
+ 'savePendingAllureResults' ,
215
+ { } ,
216
+ {
217
+ log : config . logAllureHooksEnabled ( )
218
+ }
219
+ ) . then ( ( pendingResults ) => {
220
+ logger . allure (
221
+ '########### Reinit pendingResults ################ %O' ,
222
+ pendingResults
223
+ ) ;
224
+ } ) ;
225
+ }
226
+ } ;
130
227
// Export everything
131
228
module . exports = {
132
- ALLURE_AFTER_HOOK ,
133
- ALLURE_AFTER_EACH_HOOK ,
134
- isAllureHook,
135
- clearAllureHookStepsFromTests,
136
- reorderAllureAfterHooks,
137
- reorderAllureAfterEachHooks,
138
- reorderAllureGlobalHooks,
229
+ ALLURE_HOOK_NAME ,
139
230
reorderAllureHooks,
140
- isLastRootHook,
141
- setTestIdToScreenshot
231
+ setTestIdToScreenshot,
232
+ buildPendingResults,
233
+ loadPendingResults,
234
+ clearAllureHookStepsFromTests,
235
+ clearAllureHookStepsFromHook
142
236
} ;
0 commit comments