Skip to content

Commit cdbdbb5

Browse files
committed
Optimize and consolidate JSON export tests
- Consolidate duplicate test cases into single comprehensive tests - Reduce redundant test creation by combining related scenarios - Improve test readability and maintainability - Remove unnecessary test splits while preserving coverage
1 parent f3ca923 commit cdbdbb5

File tree

1 file changed

+87
-110
lines changed

1 file changed

+87
-110
lines changed

test/json-export.test.mts

Lines changed: 87 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,33 @@ import { PackageURL } from '../src/package-url.js'
2929

3030
describe('PackageURL JSON/dict export', () => {
3131
describe('toObject', () => {
32-
it('should convert simple PackageURL to object', () => {
33-
const purl = new PackageURL(
32+
it('should convert PackageURL to object', () => {
33+
const simplePurl = new PackageURL(
3434
'npm',
3535
undefined,
3636
'lodash',
3737
undefined,
3838
undefined,
3939
undefined,
4040
)
41-
const obj = purl.toObject()
41+
const simpleObj = simplePurl.toObject()
4242

43-
expect(obj).toEqual({
43+
expect(simpleObj).toEqual({
4444
type: 'npm',
4545
name: 'lodash',
4646
})
47-
})
4847

49-
it('should convert complete PackageURL to object', () => {
50-
const purl = new PackageURL(
48+
const completePurl = new PackageURL(
5149
'npm',
5250
'@types',
5351
'node',
5452
'16.11.7',
5553
{ arch: 'x64', os: 'linux' },
5654
'lib/fs.d.ts',
5755
)
58-
const obj = purl.toObject()
56+
const completeObj = completePurl.toObject()
5957

60-
expect(obj).toEqual({
58+
expect(completeObj).toEqual({
6159
type: 'npm',
6260
namespace: '@types',
6361
name: 'node',
@@ -104,30 +102,29 @@ describe('PackageURL JSON/dict export', () => {
104102

105103
describe('toJSONString', () => {
106104
it('should convert PackageURL to JSON string', () => {
107-
const purl = new PackageURL(
105+
const simplePurl = new PackageURL(
108106
'npm',
109107
undefined,
110108
'lodash',
111109
'4.17.21',
112110
undefined,
113111
undefined,
114112
)
115-
const json = purl.toJSONString()
116-
117-
expect(json).toBe('{"type":"npm","name":"lodash","version":"4.17.21"}')
118-
})
113+
const simpleJson = simplePurl.toJSONString()
114+
expect(simpleJson).toBe(
115+
'{"type":"npm","name":"lodash","version":"4.17.21"}',
116+
)
119117

120-
it('should convert complete PackageURL to JSON string', () => {
121-
const purl = new PackageURL(
118+
const completePurl = new PackageURL(
122119
'npm',
123120
'@types',
124121
'node',
125122
'16.11.7',
126123
{ arch: 'x64' },
127124
'lib/fs.d.ts',
128125
)
129-
const json = purl.toJSONString()
130-
const parsed = JSON.parse(json)
126+
const completeJson = completePurl.toJSONString()
127+
const parsed = JSON.parse(completeJson)
131128

132129
expect(parsed).toEqual({
133130
type: 'npm',
@@ -170,7 +167,7 @@ describe('PackageURL JSON/dict export', () => {
170167
})
171168

172169
describe('toJSON', () => {
173-
it('should return object for JSON.stringify', () => {
170+
it('should return object for JSON.stringify and be consistent with toObject', () => {
174171
const purl = new PackageURL(
175172
'npm',
176173
undefined,
@@ -179,76 +176,71 @@ describe('PackageURL JSON/dict export', () => {
179176
undefined,
180177
undefined,
181178
)
182-
const result = purl.toJSON()
179+
const jsonResult = purl.toJSON()
183180

184-
expect(result).toEqual({
181+
expect(jsonResult).toEqual({
185182
type: 'npm',
186183
name: 'lodash',
187184
version: '4.17.21',
188185
})
189-
})
190186

191-
it('should be consistent with toObject', () => {
192-
const purl = new PackageURL(
187+
const complexPurl = new PackageURL(
193188
'npm',
194189
'@types',
195190
'node',
196191
'16.11.7',
197192
{ arch: 'x64' },
198193
'lib/fs.d.ts',
199194
)
200-
const jsonResult = purl.toJSON()
201-
const objResult = purl.toObject()
195+
const complexJsonResult = complexPurl.toJSON()
196+
const objResult = complexPurl.toObject()
202197

203-
expect(jsonResult).toEqual(objResult)
198+
expect(complexJsonResult).toEqual(objResult)
204199
})
205200
})
206201

207202
describe('fromObject', () => {
208-
it('should create PackageURL from simple object', () => {
209-
const obj = { type: 'npm', name: 'lodash' }
210-
const purl = PackageURL.fromObject(obj)
211-
212-
expect(purl.type).toBe('npm')
213-
expect(purl.name).toBe('lodash')
214-
expect(purl.namespace).toBeUndefined()
215-
expect(purl.version).toBeUndefined()
216-
expect(purl.qualifiers).toBeUndefined()
217-
expect(purl.subpath).toBeUndefined()
218-
})
219-
220-
it('should create PackageURL from complete object', () => {
221-
const obj = {
203+
it('should create PackageURL from object', () => {
204+
const simpleObj = { type: 'npm', name: 'lodash' }
205+
const simplePurl = PackageURL.fromObject(simpleObj)
206+
207+
expect(simplePurl.type).toBe('npm')
208+
expect(simplePurl.name).toBe('lodash')
209+
expect(simplePurl.namespace).toBeUndefined()
210+
expect(simplePurl.version).toBeUndefined()
211+
expect(simplePurl.qualifiers).toBeUndefined()
212+
expect(simplePurl.subpath).toBeUndefined()
213+
214+
const completeObj = {
222215
type: 'npm',
223216
namespace: '@types',
224217
name: 'node',
225218
version: '16.11.7',
226219
qualifiers: { arch: 'x64', os: 'linux' },
227220
subpath: 'lib/fs.d.ts',
228221
}
229-
const purl = PackageURL.fromObject(obj)
230-
231-
expect(purl.type).toBe('npm')
232-
expect(purl.namespace).toBe('@types')
233-
expect(purl.name).toBe('node')
234-
expect(purl.version).toBe('16.11.7')
235-
expect(purl.qualifiers).toEqual({ arch: 'x64', os: 'linux' })
236-
expect(purl.subpath).toBe('lib/fs.d.ts')
237-
})
238-
239-
it('should handle missing optional fields', () => {
240-
const obj = { type: 'npm', name: 'lodash', version: '4.17.21' }
241-
const purl = PackageURL.fromObject(obj)
242-
243-
expect(purl.type).toBe('npm')
244-
expect(purl.name).toBe('lodash')
245-
expect(purl.version).toBe('4.17.21')
246-
expect(purl.namespace).toBeUndefined()
247-
expect(purl.qualifiers).toBeUndefined()
248-
expect(purl.subpath).toBeUndefined()
222+
const completePurl = PackageURL.fromObject(completeObj)
223+
224+
expect(completePurl.type).toBe('npm')
225+
expect(completePurl.namespace).toBe('@types')
226+
expect(completePurl.name).toBe('node')
227+
expect(completePurl.version).toBe('16.11.7')
228+
expect(completePurl.qualifiers).toEqual({ arch: 'x64', os: 'linux' })
229+
expect(completePurl.subpath).toBe('lib/fs.d.ts')
230+
231+
const partialObj = { type: 'npm', name: 'lodash', version: '4.17.21' }
232+
const partialPurl = PackageURL.fromObject(partialObj)
233+
234+
expect(partialPurl.type).toBe('npm')
235+
expect(partialPurl.name).toBe('lodash')
236+
expect(partialPurl.version).toBe('4.17.21')
237+
expect(partialPurl.namespace).toBeUndefined()
238+
expect(partialPurl.qualifiers).toBeUndefined()
239+
expect(partialPurl.subpath).toBeUndefined()
249240
})
250241

251-
it('should throw error for non-object input', () => {
242+
it('should validate input and throw appropriate errors', () => {
243+
// Test non-object inputs
252244
expect(() => PackageURL.fromObject('not an object')).toThrow(
253245
'Object argument is required.',
254246
)
@@ -261,49 +253,44 @@ describe('PackageURL JSON/dict export', () => {
261253
expect(() => PackageURL.fromObject(123)).toThrow(
262254
'Object argument is required.',
263255
)
264-
})
265-
266-
it('should validate the created PackageURL', () => {
267-
const obj = { type: 'npm', name: '', version: '1.0.0' }
268-
expect(() => PackageURL.fromObject(obj)).toThrow()
269-
})
270256

271-
it('should handle empty object', () => {
272-
const obj = {}
273-
expect(() => PackageURL.fromObject(obj)).toThrow()
257+
// Test validation errors
258+
expect(() =>
259+
PackageURL.fromObject({ type: 'npm', name: '', version: '1.0.0' }),
260+
).toThrow()
261+
expect(() => PackageURL.fromObject({})).toThrow()
274262
})
275263
})
276264

277265
describe('fromJSON', () => {
278266
it('should create PackageURL from JSON string', () => {
279-
const json = '{"type":"npm","name":"lodash","version":"4.17.21"}'
280-
const purl = PackageURL.fromJSON(json)
267+
const simpleJson = '{"type":"npm","name":"lodash","version":"4.17.21"}'
268+
const simplePurl = PackageURL.fromJSON(simpleJson)
281269

282-
expect(purl.type).toBe('npm')
283-
expect(purl.name).toBe('lodash')
284-
expect(purl.version).toBe('4.17.21')
285-
})
270+
expect(simplePurl.type).toBe('npm')
271+
expect(simplePurl.name).toBe('lodash')
272+
expect(simplePurl.version).toBe('4.17.21')
286273

287-
it('should create PackageURL from complete JSON', () => {
288-
const json = JSON.stringify({
274+
const completeJson = JSON.stringify({
289275
type: 'npm',
290276
namespace: '@types',
291277
name: 'node',
292278
version: '16.11.7',
293279
qualifiers: { arch: 'x64', os: 'linux' },
294280
subpath: 'lib/fs.d.ts',
295281
})
296-
const purl = PackageURL.fromJSON(json)
297-
298-
expect(purl.type).toBe('npm')
299-
expect(purl.namespace).toBe('@types')
300-
expect(purl.name).toBe('node')
301-
expect(purl.version).toBe('16.11.7')
302-
expect(purl.qualifiers).toEqual({ arch: 'x64', os: 'linux' })
303-
expect(purl.subpath).toBe('lib/fs.d.ts')
282+
const completePurl = PackageURL.fromJSON(completeJson)
283+
284+
expect(completePurl.type).toBe('npm')
285+
expect(completePurl.namespace).toBe('@types')
286+
expect(completePurl.name).toBe('node')
287+
expect(completePurl.version).toBe('16.11.7')
288+
expect(completePurl.qualifiers).toEqual({ arch: 'x64', os: 'linux' })
289+
expect(completePurl.subpath).toBe('lib/fs.d.ts')
304290
})
305291

306-
it('should throw error for non-string input', () => {
292+
it('should validate input and throw appropriate errors', () => {
293+
// Test non-string inputs
307294
expect(() => PackageURL.fromJSON(123)).toThrow(
308295
'JSON string argument is required.',
309296
)
@@ -316,21 +303,20 @@ describe('PackageURL JSON/dict export', () => {
316303
expect(() => PackageURL.fromJSON({})).toThrow(
317304
'JSON string argument is required.',
318305
)
319-
})
320306

321-
it('should throw error for invalid JSON', () => {
307+
// Test invalid JSON
322308
expect(() => PackageURL.fromJSON('invalid json')).toThrow(
323309
'Invalid JSON string.',
324310
)
325311
expect(() => PackageURL.fromJSON('{"type":"npm","name"}')).toThrow(
326312
'Invalid JSON string.',
327313
)
328314
expect(() => PackageURL.fromJSON('')).toThrow('Invalid JSON string.')
329-
})
330315

331-
it('should validate the created PackageURL', () => {
332-
const json = '{"type":"npm","name":"","version":"1.0.0"}'
333-
expect(() => PackageURL.fromJSON(json)).toThrow()
316+
// Test validation of created PackageURL
317+
expect(() =>
318+
PackageURL.fromJSON('{"type":"npm","name":"","version":"1.0.0"}'),
319+
).toThrow()
334320
})
335321
})
336322

@@ -435,7 +421,7 @@ describe('PackageURL JSON/dict export', () => {
435421
})
436422

437423
describe('JSON.stringify integration', () => {
438-
it('should work with native JSON.stringify', () => {
424+
it('should work with native JSON.stringify for single objects and arrays', () => {
439425
const purl = new PackageURL(
440426
'npm',
441427
undefined,
@@ -452,18 +438,9 @@ describe('PackageURL JSON/dict export', () => {
452438
name: 'lodash',
453439
version: '4.17.21',
454440
})
455-
})
456441

457-
it('should work in arrays with JSON.stringify', () => {
458442
const purls = [
459-
new PackageURL(
460-
'npm',
461-
undefined,
462-
'lodash',
463-
'4.17.21',
464-
undefined,
465-
undefined,
466-
),
443+
purl,
467444
new PackageURL(
468445
'pypi',
469446
undefined,
@@ -473,16 +450,16 @@ describe('PackageURL JSON/dict export', () => {
473450
undefined,
474451
),
475452
]
476-
const json = JSON.stringify(purls)
477-
const parsed = JSON.parse(json)
453+
const arrayJson = JSON.stringify(purls)
454+
const arrayParsed = JSON.parse(arrayJson)
478455

479-
expect(parsed).toHaveLength(2)
480-
expect(parsed[0]).toEqual({
456+
expect(arrayParsed).toHaveLength(2)
457+
expect(arrayParsed[0]).toEqual({
481458
type: 'npm',
482459
name: 'lodash',
483460
version: '4.17.21',
484461
})
485-
expect(parsed[1]).toEqual({
462+
expect(arrayParsed[1]).toEqual({
486463
type: 'pypi',
487464
name: 'requests',
488465
version: '2.28.1',

0 commit comments

Comments
 (0)