Skip to content

Commit eeefa74

Browse files
committed
Rework date test
1 parent 6aedfff commit eeefa74

File tree

1 file changed

+127
-48
lines changed

1 file changed

+127
-48
lines changed

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

Lines changed: 127 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -452,54 +452,133 @@ describe('@mongodb-js/shell-bson-parser', function () {
452452
const actual = parse(input, options);
453453
const expectedDate = new (Date as any)(...args) as Date;
454454

455-
// Sometimes we'll get a millisecond difference between instantiating the dates, so let's correct for that
456-
const millisecondsDiff =
457-
actual.getMilliseconds - expectedDate.getMilliseconds();
458-
expect(Math.abs(millisecondsDiff)).to.be.lessThan(2);
459-
460-
expectedDate.setMilliseconds(actual.getMilliseconds);
461-
462-
expect(actual).to.deep.equal({
463-
getDate: expectedDate.getDate(),
464-
getDay: expectedDate.getDay(),
465-
getFullYear: expectedDate.getFullYear(),
466-
getHours: expectedDate.getHours(),
467-
getMilliseconds: expectedDate.getMilliseconds(),
468-
getMinutes: expectedDate.getMinutes(),
469-
getMonth: expectedDate.getMonth(),
470-
getSeconds: expectedDate.getSeconds(),
471-
getTime: expectedDate.getTime(),
472-
getTimezoneOffset: expectedDate.getTimezoneOffset(),
473-
getUTCDate: expectedDate.getUTCDate(),
474-
getUTCDay: expectedDate.getUTCDay(),
475-
getUTCFullYear: expectedDate.getUTCFullYear(),
476-
getUTCHours: expectedDate.getUTCHours(),
477-
getUTCMilliseconds: expectedDate.getUTCMilliseconds(),
478-
getUTCMinutes: expectedDate.getUTCMinutes(),
479-
getUTCMonth: expectedDate.getUTCMonth(),
480-
getUTCSeconds: expectedDate.getUTCSeconds(),
481-
getYear: (expectedDate as any).getYear(), // getYear is deprecated
482-
setDate: new Date(expectedDate).setDate(24),
483-
setFullYear: new Date(expectedDate).setFullYear(2010),
484-
setHours: new Date(expectedDate).setHours(23),
485-
setMilliseconds: new Date(expectedDate).setMilliseconds(1),
486-
setMinutes: new Date(expectedDate).setMinutes(1),
487-
setMonth: new Date(expectedDate).setMonth(1),
488-
setSeconds: new Date(expectedDate).setSeconds(59),
489-
setTime: new Date(expectedDate).setTime(10),
490-
setUTCDate: new Date(expectedDate).setUTCDate(24),
491-
setUTCFullYear: new Date(expectedDate).setUTCFullYear(2010),
492-
setUTCHours: new Date(expectedDate).setUTCHours(23),
493-
setUTCMilliseconds: new Date(expectedDate).setUTCMilliseconds(
494-
1
495-
),
496-
setUTCMinutes: new Date(expectedDate).setUTCMinutes(1),
497-
setUTCMonth: new Date(expectedDate).setUTCMonth(1),
498-
setUTCSeconds: new Date(expectedDate).setUTCSeconds(59),
499-
setYear: (new Date(expectedDate) as any).setYear(96), // setYear is deprecated
500-
toISOString: expectedDate.toISOString(),
501-
valueOf: expectedDate.valueOf(),
502-
});
455+
// When constructing a date with no arguments, it will be set to the current date,
456+
// which is prone to race conditions for millisecond precision.
457+
const allowedMillisecondDelta = args.length === 0 ? 2 : 0;
458+
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());
463+
expect(actual.getMilliseconds).to.be.approximately(
464+
expectedDate.getMilliseconds(),
465+
allowedMillisecondDelta
466+
);
467+
expect(actual.getMinutes).to.equal(expectedDate.getMinutes());
468+
expect(actual.getMonth).to.equal(expectedDate.getMonth());
469+
expect(actual.getSeconds).to.equal(expectedDate.getSeconds());
470+
expect(actual.getTime).to.be.approximately(
471+
expectedDate.getTime(),
472+
allowedMillisecondDelta
473+
);
474+
expect(actual.getTimezoneOffset).to.equal(
475+
expectedDate.getTimezoneOffset()
476+
);
477+
expect(actual.getUTCDate).to.equal(expectedDate.getUTCDate());
478+
expect(actual.getUTCDay).to.equal(expectedDate.getUTCDay());
479+
expect(actual.getUTCFullYear).to.equal(
480+
expectedDate.getUTCFullYear()
481+
);
482+
expect(actual.getUTCHours).to.equal(expectedDate.getUTCHours());
483+
expect(actual.getUTCMilliseconds).to.be.approximately(
484+
expectedDate.getUTCMilliseconds(),
485+
allowedMillisecondDelta
486+
);
487+
expect(actual.getUTCMinutes).to.equal(
488+
expectedDate.getUTCMinutes()
489+
);
490+
expect(actual.getUTCMonth).to.equal(expectedDate.getUTCMonth());
491+
expect(actual.getUTCSeconds).to.equal(
492+
expectedDate.getUTCSeconds()
493+
);
494+
expect(actual.getYear).to.equal((expectedDate as any).getYear()); // getYear is deprecated
495+
expect(actual.setDate).to.be.approximately(
496+
new Date(expectedDate).setDate(24),
497+
allowedMillisecondDelta
498+
);
499+
expect(actual.setFullYear).to.be.approximately(
500+
new Date(expectedDate).setFullYear(2010),
501+
allowedMillisecondDelta
502+
);
503+
expect(actual.setHours).to.be.approximately(
504+
new Date(expectedDate).setHours(23),
505+
allowedMillisecondDelta
506+
);
507+
expect(actual.setMilliseconds).to.be.approximately(
508+
new Date(expectedDate).setMilliseconds(1),
509+
allowedMillisecondDelta
510+
);
511+
expect(actual.setMinutes).to.be.approximately(
512+
new Date(expectedDate).setMinutes(1),
513+
allowedMillisecondDelta
514+
);
515+
expect(actual.setMonth).to.be.approximately(
516+
new Date(expectedDate).setMonth(1),
517+
allowedMillisecondDelta
518+
);
519+
expect(actual.setSeconds).to.be.approximately(
520+
new Date(expectedDate).setSeconds(59),
521+
allowedMillisecondDelta
522+
);
523+
expect(actual.setTime).to.be.approximately(
524+
new Date(expectedDate).setTime(10),
525+
allowedMillisecondDelta
526+
);
527+
expect(actual.setUTCDate).to.be.approximately(
528+
new Date(expectedDate).setUTCDate(24),
529+
allowedMillisecondDelta
530+
);
531+
expect(actual.setUTCFullYear).to.be.approximately(
532+
new Date(expectedDate).setUTCFullYear(2010),
533+
allowedMillisecondDelta
534+
);
535+
expect(actual.setUTCHours).to.be.approximately(
536+
new Date(expectedDate).setUTCHours(23),
537+
allowedMillisecondDelta
538+
);
539+
expect(actual.setUTCMilliseconds).to.be.approximately(
540+
new Date(expectedDate).setUTCMilliseconds(1),
541+
allowedMillisecondDelta
542+
);
543+
expect(actual.setUTCMinutes).to.be.approximately(
544+
new Date(expectedDate).setUTCMinutes(1),
545+
allowedMillisecondDelta
546+
);
547+
expect(actual.setUTCMonth).to.be.approximately(
548+
new Date(expectedDate).setUTCMonth(1),
549+
allowedMillisecondDelta
550+
);
551+
expect(actual.setUTCSeconds).to.be.approximately(
552+
new Date(expectedDate).setUTCSeconds(59),
553+
allowedMillisecondDelta
554+
);
555+
expect(actual.setYear).to.be.approximately(
556+
(new Date(expectedDate) as any).setYear(96),
557+
allowedMillisecondDelta
558+
); // setYear is deprecated
559+
expect(actual.valueOf).to.be.approximately(
560+
expectedDate.valueOf(),
561+
allowedMillisecondDelta
562+
);
563+
564+
const isoRegex = /^([^.]*\.)([\d]*)(Z)$/;
565+
const actualMatch = isoRegex.exec(actual.toISOString);
566+
const expectedMatch = isoRegex.exec(expectedDate.toISOString());
567+
568+
expect(actualMatch?.length).to.equal(4);
569+
expect(expectedMatch?.length).to.equal(4);
570+
571+
// Date group - 1970-01-01T00:00:00.
572+
expect(actualMatch![1]).to.equal(expectedMatch![1]);
573+
574+
// Millisecond group
575+
expect(Number.parseInt(actualMatch![2])).to.be.approximately(
576+
Number.parseInt(expectedMatch![2]),
577+
allowedMillisecondDelta
578+
);
579+
580+
// Z
581+
expect(actualMatch![3]).to.equal(expectedMatch![3]);
503582
});
504583

505584
it('should prevent invalid functions', function () {

0 commit comments

Comments
 (0)