-
-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathcircular_reference_test.js
More file actions
186 lines (153 loc) · 6.42 KB
/
Copy pathcircular_reference_test.js
File metadata and controls
186 lines (153 loc) · 6.42 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
const { expect } = require('chai')
const { safeStringify } = require('../../lib/utils')
const { createTest } = require('../../lib/mocha/test')
const { createSuite } = require('../../lib/mocha/suite')
const MochaSuite = require('mocha/lib/suite')
describe('Circular Reference Handling', function () {
describe('safeStringify utility', function () {
it('should handle objects without circular references normally', function () {
const obj = {
name: 'test',
value: 42,
nested: { prop: 'value' },
}
const result = safeStringify(obj)
const parsed = JSON.parse(result)
expect(parsed.name).to.equal('test')
expect(parsed.value).to.equal(42)
expect(parsed.nested.prop).to.equal('value')
})
it('should handle simple circular references', function () {
const obj = { name: 'test' }
obj.self = obj
const result = safeStringify(obj)
expect(result).to.not.throw
expect(result).to.contain('test')
expect(result).to.contain('Circular Reference')
})
it('should skip default problematic keys', function () {
const obj = {
name: 'test',
parent: { title: 'parent' },
tests: [{ title: 'test1' }],
suite: { title: 'suite' },
root: { title: 'root' },
ctx: { title: 'context' },
}
const result = safeStringify(obj)
const parsed = JSON.parse(result)
expect(parsed.name).to.equal('test')
expect(parsed.parent).to.be.undefined
expect(parsed.tests).to.be.undefined
expect(parsed.suite).to.be.undefined
expect(parsed.root).to.be.undefined
expect(parsed.ctx).to.be.undefined
})
it('should skip custom keys when specified', function () {
const obj = {
name: 'test',
customKey: 'should be skipped',
keepThis: 'should be kept',
}
const result = safeStringify(obj, ['customKey'])
const parsed = JSON.parse(result)
expect(parsed.name).to.equal('test')
expect(parsed.customKey).to.be.undefined
expect(parsed.keepThis).to.equal('should be kept')
})
it('should handle complex nested circular references', function () {
const parent = { name: 'parent', children: [] }
const child1 = { name: 'child1', parent: parent }
const child2 = { name: 'child2', parent: parent }
parent.children.push(child1, child2)
const result = safeStringify(parent)
expect(result).to.not.throw
expect(result).to.contain('parent')
expect(result).to.contain('children')
})
})
describe('CodeceptJS objects circular reference handling', function () {
let rootSuite, suite, test
beforeEach(function () {
rootSuite = new MochaSuite('', null, true)
suite = createSuite(rootSuite, 'Test Suite')
test = createTest('Test 1', () => {})
test.addToSuite(suite)
})
it('should handle Test objects with circular references', function () {
// Before fix: JSON.stringify(test) would throw
const result = safeStringify(test)
expect(result).to.not.throw
const parsed = JSON.parse(result)
expect(parsed.title).to.equal('Test 1')
expect(parsed.tags).to.be.an('array')
expect(parsed.codeceptjs).to.be.true
// parent should be skipped to break circular reference
expect(parsed.parent).to.be.undefined
})
it('should handle Suite objects with circular references', function () {
// Before fix: JSON.stringify(suite) would throw
const result = safeStringify(suite)
expect(result).to.not.throw
const parsed = JSON.parse(result)
expect(parsed.title).to.equal('Test Suite')
expect(parsed.codeceptjs).to.be.true
// tests should be skipped to break circular reference
expect(parsed.tests).to.be.undefined
})
it('should preserve essential Test properties while avoiding circular references', function () {
test.opts = { timeout: 5000 }
test.tags = ['@smoke']
test.meta = { feature: 'login' }
test.notes = [{ type: 'info', text: 'test note' }]
test.artifacts = ['screenshot.png']
const result = safeStringify(test)
const parsed = JSON.parse(result)
expect(parsed.opts).to.deep.equal({ timeout: 5000 })
expect(parsed.tags).to.deep.equal(['@smoke'])
expect(parsed.meta).to.deep.equal({ feature: 'login' })
expect(parsed.notes).to.deep.equal([{ type: 'info', text: 'test note' }])
expect(parsed.artifacts).to.deep.equal(['screenshot.png'])
expect(parsed.parent).to.be.undefined // Circular reference broken
})
it('should preserve essential Suite properties while avoiding circular references', function () {
suite.opts = { retries: 3 }
suite.tags = ['@feature']
const result = safeStringify(suite)
const parsed = JSON.parse(result)
expect(parsed.opts).to.deep.equal({ retries: 3 })
expect(parsed.tags).to.deep.equal(['@feature'])
expect(parsed.tests).to.be.undefined // Circular reference broken
})
it('should handle deeply nested objects with multiple circular references', function () {
// Create a more complex structure
const childSuite = createSuite(suite, 'Child Suite')
const childTest = createTest('Child Test', () => {})
childTest.addToSuite(childSuite)
const result = safeStringify(suite)
expect(result).to.not.throw
const parsed = JSON.parse(result)
expect(parsed.title).to.equal('Test Suite')
})
})
describe('Integration with existing serialization', function () {
it('should work with serializeTest function', function () {
const test = createTest('Integration Test', () => {})
const suite = createSuite(new MochaSuite('', null, true), 'Integration Suite')
test.addToSuite(suite)
// The existing serializeTest should continue to work
const serialized = test.simplify()
expect(serialized).to.be.an('object')
expect(serialized.title).to.equal('Integration Test')
expect(serialized.parent).to.be.an('object')
expect(serialized.parent.title).to.equal('Integration Suite')
})
it('should work with serializeSuite function', function () {
const suite = createSuite(new MochaSuite('', null, true), 'Integration Suite')
// The existing serializeSuite should continue to work
const serialized = suite.simplify()
expect(serialized).to.be.an('object')
expect(serialized.title).to.equal('Integration Suite')
})
})
})