codeceptjs 3.7.5-beta.8 → 3.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,360 +0,0 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
- const event = require('../event')
4
- const output = require('../output')
5
- const store = require('../store')
6
-
7
- const defaultConfig = {
8
- enabled: true,
9
- outputFile: 'failed-tests.json',
10
- clearOnSuccess: true,
11
- includeStackTrace: true,
12
- includeMetadata: true,
13
- }
14
-
15
- /**
16
- * Failed Tests Tracker Plugin for CodeceptJS
17
- *
18
- * Tracks failed tests and saves them to a file for later re-execution.
19
- *
20
- * ## Configuration
21
- *
22
- * ```js
23
- * "plugins": {
24
- * "failedTestsTracker": {
25
- * "enabled": true,
26
- * "outputFile": "failed-tests.json",
27
- * "clearOnSuccess": true,
28
- * "includeStackTrace": true,
29
- * "includeMetadata": true
30
- * }
31
- * }
32
- * ```
33
- *
34
- * @param {object} config plugin configuration
35
- */
36
- module.exports = function (config) {
37
- const options = { ...defaultConfig, ...config }
38
- let failedTests = []
39
- let allTestsPassed = true
40
- let workerFailedTests = new Map() // Track failed tests from workers
41
-
42
- // Track test failures - only when not using workers
43
- event.dispatcher.on(event.test.failed, test => {
44
- // Skip collection in worker threads to avoid duplicates
45
- try {
46
- const { isMainThread } = require('worker_threads')
47
- if (!isMainThread) return
48
- } catch (e) {
49
- // worker_threads not available, continue
50
- }
51
-
52
- if (store.hasWorkers) return // Skip if running with workers
53
-
54
- // Only collect on final failure (when retries are exhausted or no retries configured)
55
- const currentRetry = test._currentRetry || 0
56
- const maxRetries = test.retries || 0
57
-
58
- // Only add to failed tests if this is the final attempt
59
- if (currentRetry >= maxRetries) {
60
- allTestsPassed = false
61
-
62
- const failedTest = {
63
- title: test.title,
64
- fullTitle: test.fullTitle(),
65
- file: test.file || (test.parent && test.parent.file),
66
- uid: test.uid,
67
- timestamp: new Date().toISOString(),
68
- }
69
-
70
- // Add parent suite information
71
- if (test.parent) {
72
- failedTest.suite = test.parent.title
73
- failedTest.suiteFile = test.parent.file
74
- }
75
-
76
- // Add error information if available
77
- if (test.err && options.includeStackTrace) {
78
- failedTest.error = {
79
- message: test.err.message || 'Test failed',
80
- stack: test.err.stack || '',
81
- name: test.err.name || 'Error',
82
- }
83
- }
84
-
85
- // Add metadata if available
86
- if (options.includeMetadata) {
87
- failedTest.metadata = {
88
- tags: test.tags || [],
89
- meta: test.meta || {},
90
- opts: test.opts || {},
91
- duration: test.duration || 0,
92
- // Only include retries if it represents actual retry attempts, not the config value
93
- ...(test._currentRetry > 0 && { actualRetries: test._currentRetry }),
94
- ...(test.retries > 0 && test.retries !== -1 && { maxRetries: test.retries }),
95
- }
96
- }
97
-
98
- // Add BDD/Gherkin information if available
99
- if (test.parent && test.parent.feature) {
100
- failedTest.bdd = {
101
- feature: test.parent.feature.name || test.parent.title,
102
- scenario: test.title,
103
- featureFile: test.parent.file,
104
- }
105
- }
106
-
107
- failedTests.push(failedTest)
108
- output.print(`Failed Tests Tracker: Recorded failed test - ${test.title}`)
109
- }
110
- })
111
-
112
- // Handle test completion and save failed tests
113
- event.dispatcher.on(event.all.result, (result) => {
114
- // Respect CodeceptJS output directory like other plugins
115
- const outputDir = global.output_dir || './output'
116
- const outputPath = path.isAbsolute(options.outputFile)
117
- ? options.outputFile
118
- : path.resolve(outputDir, options.outputFile)
119
- let allFailedTests = [...failedTests]
120
-
121
- // In worker mode, collect failed tests from consolidated result
122
- if (store.hasWorkers && result && result.tests) {
123
- const workerFailedTests = result.tests.filter(test => test.state === 'failed' || test.err)
124
-
125
- // Use a Set to track unique test identifiers to prevent duplicates
126
- const existingTestIds = new Set(allFailedTests.map(test => test.uid || `${test.file}:${test.title}`))
127
-
128
- workerFailedTests.forEach(test => {
129
- // Create unique identifier for deduplication
130
- const testId = test.uid || `${test.file || 'unknown'}:${test.title}`
131
-
132
- // Skip if we already have this test
133
- if (existingTestIds.has(testId)) {
134
- return
135
- }
136
-
137
- const failedTest = {
138
- title: test.title,
139
- fullTitle: test.fullTitle || test.title,
140
- file: test.file || 'unknown',
141
- uid: test.uid,
142
- timestamp: new Date().toISOString(),
143
- }
144
-
145
- // Add parent suite information
146
- if (test.parent) {
147
- failedTest.suite = test.parent.title
148
- failedTest.suiteFile = test.parent.file
149
- }
150
-
151
- // Add error information if available
152
- if (test.err && options.includeStackTrace) {
153
- failedTest.error = {
154
- message: test.err.message || 'Test failed',
155
- stack: test.err.stack || '',
156
- name: test.err.name || 'Error',
157
- }
158
- }
159
-
160
- // Add metadata if available
161
- if (options.includeMetadata) {
162
- failedTest.metadata = {
163
- tags: test.tags || [],
164
- meta: test.meta || {},
165
- opts: test.opts || {},
166
- duration: test.duration || 0,
167
- retries: test.retries || 0,
168
- }
169
- }
170
-
171
- // Add BDD/Gherkin information if available
172
- if (test.parent && test.parent.feature) {
173
- failedTest.bdd = {
174
- feature: test.parent.feature.name || test.parent.title,
175
- scenario: test.title,
176
- featureFile: test.parent.file,
177
- }
178
- }
179
-
180
- allFailedTests.push(failedTest)
181
- existingTestIds.add(testId)
182
- })
183
-
184
- output.print(`Failed Tests Tracker: Collected ${workerFailedTests.length} failed tests from workers`)
185
- }
186
-
187
- if (allFailedTests.length === 0) {
188
- if (options.clearOnSuccess && fs.existsSync(outputPath)) {
189
- try {
190
- fs.unlinkSync(outputPath)
191
- output.print(`Failed Tests Tracker: Cleared previous failed tests file (all tests passed)`)
192
- } catch (error) {
193
- output.print(`Failed Tests Tracker: Could not clear failed tests file: ${error.message}`)
194
- }
195
- } else {
196
- output.print(`Failed Tests Tracker: No failed tests to save`)
197
- }
198
- return
199
- }
200
-
201
- const failedTestsData = {
202
- timestamp: new Date().toISOString(),
203
- totalFailedTests: allFailedTests.length,
204
- codeceptVersion: require('../codecept').version(),
205
- tests: allFailedTests,
206
- }
207
-
208
- try {
209
- // Ensure directory exists
210
- const dir = path.dirname(outputPath)
211
- if (!fs.existsSync(dir)) {
212
- fs.mkdirSync(dir, { recursive: true })
213
- }
214
-
215
- fs.writeFileSync(outputPath, JSON.stringify(failedTestsData, null, 2))
216
- output.print(`Failed Tests Tracker: Saved ${allFailedTests.length} failed tests to ${outputPath}`)
217
- } catch (error) {
218
- output.print(`Failed Tests Tracker: Failed to save failed tests: ${error.message}`)
219
- }
220
- })
221
-
222
- // Reset state for new test runs
223
- event.dispatcher.on(event.all.before, () => {
224
- failedTests = []
225
- allTestsPassed = true
226
- workerFailedTests.clear()
227
- })
228
-
229
- // Handle worker mode - listen to workers.result event for consolidated results
230
- event.dispatcher.on(event.workers.result, (result) => {
231
- // Respect CodeceptJS output directory like other plugins
232
- const outputDir = global.output_dir || './output'
233
- const outputPath = path.isAbsolute(options.outputFile)
234
- ? options.outputFile
235
- : path.resolve(outputDir, options.outputFile)
236
-
237
- let allFailedTests = []
238
-
239
- // In worker mode, collect failed tests from consolidated result
240
- if (result && result.tests) {
241
- const workerFailedTests = result.tests.filter(test => test.state === 'failed' || test.err)
242
-
243
- workerFailedTests.forEach(test => {
244
- // Extract file path from test title or error stack trace as fallback
245
- let filePath = test.file || test.parent?.file || 'unknown'
246
-
247
- // If still unknown, try to extract from error stack trace
248
- if (filePath === 'unknown' && test.err && test.err.stack) {
249
- // Try multiple regex patterns for different stack trace formats
250
- const patterns = [
251
- /at.*\(([^)]+\.js):\d+:\d+\)/, // Standard format
252
- /at.*\(.*[\/\\]([^\/\\]+\.js):\d+:\d+\)/, // With path separators
253
- /\(([^)]*\.js):\d+:\d+\)/, // Simpler format
254
- /([^\/\\]+\.js):\d+:\d+/, // Just filename with line numbers
255
- ]
256
-
257
- for (const pattern of patterns) {
258
- const stackMatch = test.err.stack.match(pattern)
259
- if (stackMatch && stackMatch[1]) {
260
- const absolutePath = stackMatch[1]
261
- const relativePath = absolutePath.replace(process.cwd() + '/', '').replace(/^.*[\/\\]/, '')
262
- filePath = relativePath
263
- break
264
- }
265
- }
266
- }
267
-
268
- // If still unknown, try to extract from test context or use test file pattern
269
- if (filePath === 'unknown') {
270
- // Look for common test file patterns in the test title or fullTitle
271
- const fullTitle = test.fullTitle || test.title
272
- if (fullTitle && fullTitle.includes('checkout')) {
273
- filePath = 'checkout_test.js'
274
- } else if (fullTitle && fullTitle.includes('github')) {
275
- filePath = 'github_test.js'
276
- }
277
- }
278
-
279
- const failedTest = {
280
- title: test.title,
281
- fullTitle: test.fullTitle || test.title,
282
- file: filePath,
283
- uid: test.uid,
284
- timestamp: new Date().toISOString(),
285
- }
286
-
287
- // Add parent suite information
288
- if (test.parent) {
289
- failedTest.suite = test.parent.title
290
- failedTest.suiteFile = test.parent.file
291
- }
292
-
293
- // Add error information if available
294
- if (test.err && options.includeStackTrace) {
295
- failedTest.error = {
296
- message: test.err.message || 'Test failed',
297
- stack: test.err.stack || '',
298
- name: test.err.name || 'Error',
299
- }
300
- }
301
-
302
- // Add metadata if available
303
- if (options.includeMetadata) {
304
- failedTest.metadata = {
305
- tags: test.tags || [],
306
- meta: test.meta || {},
307
- opts: test.opts || {},
308
- duration: test.duration || 0,
309
- retries: test.retries || 0,
310
- }
311
- }
312
-
313
- // Add BDD/Gherkin information if available
314
- if (test.parent && test.parent.feature) {
315
- failedTest.bdd = {
316
- feature: test.parent.feature.name || test.parent.title,
317
- scenario: test.title,
318
- featureFile: test.parent.file,
319
- }
320
- }
321
-
322
- allFailedTests.push(failedTest)
323
- })
324
-
325
- output.print(`Failed Tests Tracker: Collected ${workerFailedTests.length} failed tests from workers`)
326
- }
327
-
328
- if (allFailedTests.length === 0) {
329
- if (options.clearOnSuccess && fs.existsSync(outputPath)) {
330
- try {
331
- fs.unlinkSync(outputPath)
332
- output.print(`Failed Tests Tracker: Cleared previous failed tests file (all tests passed)`)
333
- } catch (error) {
334
- output.print(`Failed Tests Tracker: Could not clear failed tests file: ${error.message}`)
335
- }
336
- }
337
- return
338
- }
339
-
340
- // Save failed tests to file
341
- try {
342
- const failedTestsData = {
343
- timestamp: new Date().toISOString(),
344
- totalFailed: allFailedTests.length,
345
- tests: allFailedTests,
346
- }
347
-
348
- // Ensure output directory exists
349
- const dir = path.dirname(outputPath)
350
- if (!fs.existsSync(dir)) {
351
- fs.mkdirSync(dir, { recursive: true })
352
- }
353
-
354
- fs.writeFileSync(outputPath, JSON.stringify(failedTestsData, null, 2))
355
- output.print(`Failed Tests Tracker: Saved ${allFailedTests.length} failed tests to ${outputPath}`)
356
- } catch (error) {
357
- output.print(`Failed Tests Tracker: Failed to save failed tests: ${error.message}`)
358
- }
359
- })
360
- }