Skip to content

Commit 3775544

Browse files
committed
fix(test): correct assertions and remove duplicate tests
- Remove duplicate test in safer-buffer.test.mts - Add missing .toThrow() assertions to "Invalid calls throw" test - Fix incomplete expect() statements (add .toBe(true) to ok assertions) - Remove invalid expect() calls with two arguments but no matcher - Add explanatory comment for Windows platform skip in date.test.mts These fixes ensure tests properly validate behavior and don't silently pass when they should fail.
1 parent 945dc47 commit 3775544

File tree

2 files changed

+45
-72
lines changed

2 files changed

+45
-72
lines changed

test/npm/date.test.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe('date', () => {
9393
})
9494
})
9595

96+
// Date string formatting is platform-dependent and may produce different
97+
// output on Windows due to timezone and locale differences.
9698
describe(
9799
'Date string formatting',
98100
{ skip: process.platform === 'win32' },

test/npm/safer-buffer.test.mts

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -224,142 +224,142 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
224224
for (const impl of implementations) {
225225
expect(() => {
226226
impl.Buffer.from(0)
227-
})
227+
}).toThrow()
228228
expect(() => {
229229
impl.Buffer.from(10)
230-
})
230+
}).toThrow()
231231
expect(() => {
232232
impl.Buffer.from(10, 'utf-8')
233-
})
233+
}).toThrow()
234234
expect(() => {
235235
impl.Buffer.from('string', 'invalid encoding')
236-
})
236+
}).toThrow()
237237
expect(() => {
238238
impl.Buffer.from(-10)
239-
})
239+
}).toThrow()
240240
expect(() => {
241241
impl.Buffer.from(1e90)
242-
})
242+
}).toThrow()
243243
expect(() => {
244244
impl.Buffer.from(Number.POSITIVE_INFINITY)
245-
})
245+
}).toThrow()
246246
expect(() => {
247247
impl.Buffer.from(Number.NEGATIVE_INFINITY)
248-
})
248+
}).toThrow()
249249
expect(() => {
250250
impl.Buffer.from(Number.NaN)
251-
})
251+
}).toThrow()
252252
expect(() => {
253253
impl.Buffer.from(null)
254-
})
254+
}).toThrow()
255255
expect(() => {
256256
impl.Buffer.from(undefined)
257-
})
257+
}).toThrow()
258258
expect(() => {
259259
impl.Buffer.from()
260-
})
260+
}).toThrow()
261261
expect(() => {
262262
impl.Buffer.from({})
263-
})
263+
}).toThrow()
264264
expect(() => {
265265
impl.Buffer.alloc('')
266-
})
266+
}).toThrow()
267267
expect(() => {
268268
impl.Buffer.alloc('string')
269-
})
269+
}).toThrow()
270270
expect(() => {
271271
impl.Buffer.alloc('string', 'utf-8')
272-
})
272+
}).toThrow()
273273
expect(() => {
274274
impl.Buffer.alloc('b25ldHdvdGhyZWU=', 'base64')
275-
})
275+
}).toThrow()
276276
expect(() => {
277277
impl.Buffer.alloc(-10)
278-
})
278+
}).toThrow()
279279
expect(() => {
280280
impl.Buffer.alloc(1e90)
281-
})
281+
}).toThrow()
282282
// Modern builtin Buffer.alloc does NOT throw.
283283
// https://github.com/ChALkeR/safer-buffer/issues/16
284284
expect(() => {
285285
impl.Buffer.alloc(2 * (1 << 30))
286286
}).not.toThrow()
287287
expect(() => {
288288
impl.Buffer.alloc(Number.POSITIVE_INFINITY)
289-
})
289+
}).toThrow()
290290
expect(() => {
291291
impl.Buffer.alloc(Number.NEGATIVE_INFINITY)
292-
})
292+
}).toThrow()
293293
expect(() => {
294294
impl.Buffer.alloc(null)
295-
})
295+
}).toThrow()
296296
expect(() => {
297297
impl.Buffer.alloc(undefined)
298-
})
298+
}).toThrow()
299299
expect(() => {
300300
impl.Buffer.alloc()
301301
}).toThrow()
302302
expect(() => {
303303
impl.Buffer.alloc([])
304-
})
304+
}).toThrow()
305305
expect(() => {
306306
impl.Buffer.alloc([0, 42, 3])
307-
})
307+
}).toThrow()
308308
expect(() => {
309309
impl.Buffer.alloc({})
310-
})
310+
}).toThrow()
311311
}
312312
for (const method of ['allocUnsafe', 'allocUnsafeSlow']) {
313313
expect(() => {
314314
dangerous.Buffer[method]('')
315-
})
315+
}).toThrow()
316316
expect(() => {
317317
dangerous.Buffer[method]('string')
318-
})
318+
}).toThrow()
319319
expect(() => {
320320
dangerous.Buffer[method]('string', 'utf-8')
321-
})
321+
}).toThrow()
322322
// Modern builtin Buffer.allocUnsafe and Buffer.allocUnsafeSlow do NOT throw.
323323
// https://github.com/ChALkeR/safer-buffer/issues/16
324324
expect(() => {
325325
dangerous.Buffer[method](2 * (1 << 30))
326326
}).not.toThrow()
327327
expect(() => {
328328
dangerous.Buffer[method](Number.POSITIVE_INFINITY)
329-
})
329+
}).toThrow()
330330
if (dangerous.Buffer[method] === buffer.Buffer.allocUnsafe) {
331331
logger.info(
332332
'Skipping, older impl of allocUnsafe coerced negative sizes to 0',
333333
)
334334
} else {
335335
expect(() => {
336336
dangerous.Buffer[method](-10)
337-
})
337+
}).toThrow()
338338
expect(() => {
339339
dangerous.Buffer[method](-1e90)
340-
})
340+
}).toThrow()
341341
expect(() => {
342342
dangerous.Buffer[method](Number.NEGATIVE_INFINITY)
343-
})
343+
}).toThrow()
344344
}
345345
expect(() => {
346346
dangerous.Buffer[method](null)
347-
})
347+
}).toThrow()
348348
expect(() => {
349349
dangerous.Buffer[method](undefined)
350-
})
350+
}).toThrow()
351351
expect(() => {
352352
dangerous.Buffer[method]()
353-
})
353+
}).toThrow()
354354
expect(() => {
355355
dangerous.Buffer[method]([])
356-
})
356+
}).toThrow()
357357
expect(() => {
358358
dangerous.Buffer[method]([0, 42, 3])
359-
})
359+
}).toThrow()
360360
expect(() => {
361361
dangerous.Buffer[method]({})
362-
})
362+
}).toThrow()
363363
}
364364
})
365365

@@ -382,8 +382,6 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
382382
})
383383

384384
it('Buffers have appropriate lengths (2)', () => {
385-
expect(safer.Buffer.alloc, safer.Buffer.alloc)
386-
expect(safer.Buffer.alloc, dangerous.Buffer.alloc)
387385
let ok = true
388386
for (const method of [
389387
safer.Buffer.alloc,
@@ -401,12 +399,10 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
401399
}
402400
}
403401
}
404-
expect(ok)
402+
expect(ok).toBe(true)
405403
})
406404

407405
it('.alloc(size) is zero-filled and has correct length', () => {
408-
expect(safer.Buffer.alloc, safer.Buffer.alloc)
409-
expect(safer.Buffer.alloc, dangerous.Buffer.alloc)
410406
let ok = true
411407
for (let i = 0; i < 1e2; i += 1) {
412408
const length = Math.round(Math.random() * 2e6)
@@ -431,7 +427,7 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
431427
}
432428
}
433429
}
434-
expect(ok)
430+
expect(ok).toBe(true)
435431
})
436432

437433
it('.allocUnsafe / .allocUnsafeSlow are fillable and have correct lengths', () => {
@@ -461,36 +457,11 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
461457
}
462458
}
463459
}
464-
expect(ok, method)
465-
}
466-
})
467-
468-
it('.alloc(size, fill) is `fill`-filled', () => {
469-
expect(safer.Buffer.alloc, safer.Buffer.alloc)
470-
expect(safer.Buffer.alloc, dangerous.Buffer.alloc)
471-
let ok = true
472-
for (let i = 0; i < 1e2; i += 1) {
473-
const length = Math.round(Math.random() * 2e6)
474-
const fill = Math.round(Math.random() * 255)
475-
const buf = safer.Buffer.alloc(length, fill)
476-
if (!buffer.Buffer.isBuffer(buf)) {
477-
ok = false
478-
}
479-
if (buf.length !== length) {
480-
ok = false
481-
}
482-
for (let j = 0; j < length; j += 1) {
483-
if (buf[j] !== fill) {
484-
ok = false
485-
}
486-
}
460+
expect(ok).toBe(true)
487461
}
488-
expect(ok)
489462
})
490463

491464
it('.alloc(size, fill) is `fill`-filled', () => {
492-
expect(safer.Buffer.alloc, safer.Buffer.alloc)
493-
expect(safer.Buffer.alloc, dangerous.Buffer.alloc)
494465
let ok = true
495466
for (let i = 0; i < 1e2; i += 1) {
496467
const length = Math.round(Math.random() * 2e6)
@@ -508,7 +479,7 @@ describe(`${eco} > ${sockRegPkgName}`, { skip }, () => {
508479
}
509480
}
510481
}
511-
expect(ok)
482+
expect(ok).toBe(true)
512483
expect(safer.Buffer.alloc(9, 'a')).toEqual(safer.Buffer.alloc(9, 97))
513484
expect(safer.Buffer.alloc(9, 'a')).not.toEqual(safer.Buffer.alloc(9, 98))
514485

0 commit comments

Comments
 (0)