Skip to content

Commit 46da80b

Browse files
committed
revert to always creating a new date
1 parent eeefa74 commit 46da80b

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

packages/shell-bson-parser/src/index.spec.ts

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -450,120 +450,145 @@ describe('@mongodb-js/shell-bson-parser', function () {
450450
}`;
451451

452452
const actual = parse(input, options);
453-
const expectedDate = new (Date as any)(...args) as Date;
454453

455454
// When constructing a date with no arguments, it will be set to the current date,
456455
// which is prone to race conditions for millisecond precision.
457456
const allowedMillisecondDelta = args.length === 0 ? 2 : 0;
458457

459-
expect(actual.getDate).to.equal(expectedDate.getDate());
460-
expect(actual.getDay).to.equal(expectedDate.getDay());
461-
expect(actual.getFullYear).to.equal(expectedDate.getFullYear());
462-
expect(actual.getHours).to.equal(expectedDate.getHours());
458+
expect(actual.getDate).to.equal(
459+
new (Date as any)(...args).getDate()
460+
);
461+
expect(actual.getDay).to.equal(
462+
new (Date as any)(...args).getDay()
463+
);
464+
expect(actual.getFullYear).to.equal(
465+
new (Date as any)(...args).getFullYear()
466+
);
467+
expect(actual.getHours).to.equal(
468+
new (Date as any)(...args).getHours()
469+
);
463470
expect(actual.getMilliseconds).to.be.approximately(
464-
expectedDate.getMilliseconds(),
471+
new (Date as any)(...args).getMilliseconds(),
465472
allowedMillisecondDelta
466473
);
467-
expect(actual.getMinutes).to.equal(expectedDate.getMinutes());
468-
expect(actual.getMonth).to.equal(expectedDate.getMonth());
469-
expect(actual.getSeconds).to.equal(expectedDate.getSeconds());
474+
expect(actual.getMinutes).to.equal(
475+
new (Date as any)(...args).getMinutes()
476+
);
477+
expect(actual.getMonth).to.equal(
478+
new (Date as any)(...args).getMonth()
479+
);
480+
expect(actual.getSeconds).to.equal(
481+
new (Date as any)(...args).getSeconds()
482+
);
470483
expect(actual.getTime).to.be.approximately(
471-
expectedDate.getTime(),
484+
new (Date as any)(...args).getTime(),
472485
allowedMillisecondDelta
473486
);
474487
expect(actual.getTimezoneOffset).to.equal(
475-
expectedDate.getTimezoneOffset()
488+
new (Date as any)(...args).getTimezoneOffset()
489+
);
490+
expect(actual.getUTCDate).to.equal(
491+
new (Date as any)(...args).getUTCDate()
492+
);
493+
expect(actual.getUTCDay).to.equal(
494+
new (Date as any)(...args).getUTCDay()
476495
);
477-
expect(actual.getUTCDate).to.equal(expectedDate.getUTCDate());
478-
expect(actual.getUTCDay).to.equal(expectedDate.getUTCDay());
479496
expect(actual.getUTCFullYear).to.equal(
480-
expectedDate.getUTCFullYear()
497+
new (Date as any)(...args).getUTCFullYear()
498+
);
499+
expect(actual.getUTCHours).to.equal(
500+
new (Date as any)(...args).getUTCHours()
481501
);
482-
expect(actual.getUTCHours).to.equal(expectedDate.getUTCHours());
483502
expect(actual.getUTCMilliseconds).to.be.approximately(
484-
expectedDate.getUTCMilliseconds(),
503+
new (Date as any)(...args).getUTCMilliseconds(),
485504
allowedMillisecondDelta
486505
);
487506
expect(actual.getUTCMinutes).to.equal(
488-
expectedDate.getUTCMinutes()
507+
new (Date as any)(...args).getUTCMinutes()
508+
);
509+
expect(actual.getUTCMonth).to.equal(
510+
new (Date as any)(...args).getUTCMonth()
489511
);
490-
expect(actual.getUTCMonth).to.equal(expectedDate.getUTCMonth());
491512
expect(actual.getUTCSeconds).to.equal(
492-
expectedDate.getUTCSeconds()
513+
new (Date as any)(...args).getUTCSeconds()
493514
);
494-
expect(actual.getYear).to.equal((expectedDate as any).getYear()); // getYear is deprecated
515+
expect(actual.getYear).to.equal(
516+
(new (Date as any)(...args) as any).getYear()
517+
); // getYear is deprecated
495518
expect(actual.setDate).to.be.approximately(
496-
new Date(expectedDate).setDate(24),
519+
new (Date as any)(...args).setDate(24),
497520
allowedMillisecondDelta
498521
);
499522
expect(actual.setFullYear).to.be.approximately(
500-
new Date(expectedDate).setFullYear(2010),
523+
new (Date as any)(...args).setFullYear(2010),
501524
allowedMillisecondDelta
502525
);
503526
expect(actual.setHours).to.be.approximately(
504-
new Date(expectedDate).setHours(23),
527+
new (Date as any)(...args).setHours(23),
505528
allowedMillisecondDelta
506529
);
507530
expect(actual.setMilliseconds).to.be.approximately(
508-
new Date(expectedDate).setMilliseconds(1),
531+
new (Date as any)(...args).setMilliseconds(1),
509532
allowedMillisecondDelta
510533
);
511534
expect(actual.setMinutes).to.be.approximately(
512-
new Date(expectedDate).setMinutes(1),
535+
new (Date as any)(...args).setMinutes(1),
513536
allowedMillisecondDelta
514537
);
515538
expect(actual.setMonth).to.be.approximately(
516-
new Date(expectedDate).setMonth(1),
539+
new (Date as any)(...args).setMonth(1),
517540
allowedMillisecondDelta
518541
);
519542
expect(actual.setSeconds).to.be.approximately(
520-
new Date(expectedDate).setSeconds(59),
543+
new (Date as any)(...args).setSeconds(59),
521544
allowedMillisecondDelta
522545
);
523546
expect(actual.setTime).to.be.approximately(
524-
new Date(expectedDate).setTime(10),
547+
new (Date as any)(...args).setTime(10),
525548
allowedMillisecondDelta
526549
);
527550
expect(actual.setUTCDate).to.be.approximately(
528-
new Date(expectedDate).setUTCDate(24),
551+
new (Date as any)(...args).setUTCDate(24),
529552
allowedMillisecondDelta
530553
);
531554
expect(actual.setUTCFullYear).to.be.approximately(
532-
new Date(expectedDate).setUTCFullYear(2010),
555+
new (Date as any)(...args).setUTCFullYear(2010),
533556
allowedMillisecondDelta
534557
);
535558
expect(actual.setUTCHours).to.be.approximately(
536-
new Date(expectedDate).setUTCHours(23),
559+
new (Date as any)(...args).setUTCHours(23),
537560
allowedMillisecondDelta
538561
);
539562
expect(actual.setUTCMilliseconds).to.be.approximately(
540-
new Date(expectedDate).setUTCMilliseconds(1),
563+
new (Date as any)(...args).setUTCMilliseconds(1),
541564
allowedMillisecondDelta
542565
);
543566
expect(actual.setUTCMinutes).to.be.approximately(
544-
new Date(expectedDate).setUTCMinutes(1),
567+
new (Date as any)(...args).setUTCMinutes(1),
545568
allowedMillisecondDelta
546569
);
547570
expect(actual.setUTCMonth).to.be.approximately(
548-
new Date(expectedDate).setUTCMonth(1),
571+
new (Date as any)(...args).setUTCMonth(1),
549572
allowedMillisecondDelta
550573
);
551574
expect(actual.setUTCSeconds).to.be.approximately(
552-
new Date(expectedDate).setUTCSeconds(59),
575+
new (Date as any)(...args).setUTCSeconds(59),
553576
allowedMillisecondDelta
554577
);
555578
expect(actual.setYear).to.be.approximately(
556-
(new Date(expectedDate) as any).setYear(96),
579+
(new (Date as any)(...args) as any).setYear(96),
557580
allowedMillisecondDelta
558581
); // setYear is deprecated
559582
expect(actual.valueOf).to.be.approximately(
560-
expectedDate.valueOf(),
583+
new (Date as any)(...args).valueOf(),
561584
allowedMillisecondDelta
562585
);
563586

564587
const isoRegex = /^([^.]*\.)([\d]*)(Z)$/;
565588
const actualMatch = isoRegex.exec(actual.toISOString);
566-
const expectedMatch = isoRegex.exec(expectedDate.toISOString());
589+
const expectedMatch = isoRegex.exec(
590+
new (Date as any)(...args).toISOString()
591+
);
567592

568593
expect(actualMatch?.length).to.equal(4);
569594
expect(expectedMatch?.length).to.equal(4);

0 commit comments

Comments
 (0)