-
-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathtest.js
More file actions
199 lines (176 loc) · 5.45 KB
/
test.js
File metadata and controls
199 lines (176 loc) · 5.45 KB
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
const Test = require('mocha/lib/test')
const Suite = require('mocha/lib/suite')
const { test: testWrapper } = require('./asyncWrapper')
const { enhanceMochaSuite, createSuite } = require('./suite')
const { genTestId, serializeError, clearString, relativeDir } = require('../utils')
const Step = require('../step/base')
/**
* Factory function to create enhanced tests
* @param {string} title - Test title
* @param {Function} fn - Test function
* @returns {CodeceptJS.Test & Mocha.Test} New enhanced test instance
*/
function createTest(title, fn) {
const test = new Test(title, fn)
return enhanceMochaTest(test)
}
/**
* Enhances Mocha Test with CodeceptJS specific functionality using composition
* @param {CodeceptJS.Test & Mocha.Test} test - Test instance to enhance
* @returns {CodeceptJS.Test & Mocha.Test} Enhanced test instance
*/
function enhanceMochaTest(test) {
// if no test, create a dummy one
if (!test) test = createTest('...', () => {})
// already enhanced
if (test.codeceptjs) return test
test.codeceptjs = true
// Add properties
test.tags = test.title.match(/(\@[a-zA-Z0-9-_]+)/g) || []
test.steps = []
test.config = {}
test.artifacts = []
test.inject = {}
test.opts = {}
test.meta = {}
test.retryHistory = [] // Track retry attempts
test.notes = []
test.addNote = (type, note) => {
test.notes.push({ type, text: note })
}
/**
* Records a retry attempt
* @param {boolean} passed - Whether the attempt passed
* @param {number} duration - Duration of the attempt in ms
* @param {Error} [error] - Error if the attempt failed
*/
test.recordRetryAttempt = function (passed, duration, error = null) {
test.retryHistory.push({
passed,
duration,
error: error ? error.message : null,
timestamp: new Date().toISOString(),
})
}
// Add new methods
/**
* @param {Mocha.Suite} suite - The Mocha suite to add this test to
*/
test.addToSuite = function (suite) {
enhanceMochaSuite(suite)
suite.addTest(testWrapper(this))
if (test.file && !suite.file) suite.file = test.file
test.tags = [...(test.tags || []), ...(suite.tags || [])]
test.fullTitle = () => `${suite.title}: ${test.title}`
test.uid = genTestId(test)
}
test.applyOptions = function (opts) {
if (!opts) opts = {}
test.opts = opts
test.meta = opts.meta || {}
test.totalTimeout = opts.timeout
if (opts.retries) this.retries(opts.retries)
}
test.simplify = function () {
return serializeTest(this)
}
return test
}
function deserializeTest(test) {
test = Object.assign(
createTest(test.title || '', () => {}),
test,
)
test.parent = Object.assign(new Suite(test.parent?.title || 'Suite'), test.parent)
enhanceMochaSuite(test.parent)
if (test.steps) test.steps = test.steps.map(step => Object.assign(new Step(step.title), step))
// Restore the custom fullTitle function to maintain consistency with original test
if (test.parent) {
test.fullTitle = () => `${test.parent.title}: ${test.title}`
}
return test
}
function serializeTest(test, error = null) {
// test = { ...test }
if (test.start && !test.duration) {
const end = +new Date()
test.duration = end - test.start
}
let err
if (test.err) {
err = serializeError(test.err)
test.state = 'failed'
} else if (error) {
err = serializeError(error)
test.state = 'failed'
}
const parent = {}
if (test.parent) {
parent.title = test.parent.title
}
if (test.opts) {
Object.keys(test.opts).forEach(k => {
if (typeof test.opts[k] === 'object') delete test.opts[k]
if (typeof test.opts[k] === 'function') delete test.opts[k]
})
}
let steps = undefined
if (Array.isArray(test.steps)) {
steps = test.steps.map(step => (step.simplify ? step.simplify() : step))
}
return {
opts: test.opts || {},
tags: test.tags || [],
uid: test.uid,
retries: test._retries,
retryHistory: test.retryHistory || [],
workerIndex: test.workerIndex,
title: test.title,
state: test.state,
notes: test.notes || [],
meta: test.meta || {},
artifacts: test.artifacts || {},
duration: test.duration || 0,
err,
parent,
steps,
}
}
function cloneTest(test) {
return deserializeTest(serializeTest(test))
}
/**
* Get a filename from the test object
* @param {CodeceptJS.Test} test
* @param {Object} options
* @param {string} options.suffix Add a suffix to the filename
* @param {boolean} options.unique Add a unique suffix to the file
*
* @returns {string} the filename
*/
function testToFileName(test, { suffix = '', unique = false } = {}) {
let fileName = test.title
if (unique) fileName = `${fileName}_${test?.uid || Math.floor(new Date().getTime() / 1000)}`
if (suffix) fileName = `${fileName}_${suffix}`
// remove tags with empty string (disable for now)
// fileName = fileName.replace(/\@\w+/g, '')
fileName = fileName.slice(0, 100)
if (fileName.indexOf('{') !== -1) {
fileName = fileName.substr(0, fileName.indexOf('{') - 3).trim()
}
if (test.ctx && test.ctx.test && test.ctx.test.type === 'hook') fileName = clearString(`${test.title}_${test.ctx.test.title}`)
// TODO: add suite title to file name
// if (test.parent && test.parent.title) {
// fileName = `${clearString(test.parent.title)}_${fileName}`
// }
fileName = clearString(fileName).slice(0, 100)
return fileName
}
module.exports = {
createTest,
testToFileName,
enhanceMochaTest,
serializeTest,
deserializeTest,
cloneTest,
}