andrewplummer / Sugar

A Javascript library for working with native objects.
https://sugarjs.com/
MIT License
4.53k stars 305 forks source link

Sugar.Date.get throwing "Invalid Date" when running on 2 tests in jest #672

Open Den368 opened 3 years ago

Den368 commented 3 years ago

something.ts

import * as Sugar from "sugar";
Sugar.Date.getLocale("en").addFormat("the {edge} of the year {yyyy}");

export function failingBehaivor(date: string, comparisonDate: string) {
    console.log("date, comparisonDate:", date, comparisonDate);
    const parsedDate = Sugar.Date.create(date);
    let parsedComparisonDate = Sugar.Date.get(parsedDate, comparisonDate);
    console.log("parsedComparisonDate:", parsedComparisonDate);
    return parsedComparisonDate;
}

jest test file: something.test.ts

import { failingBehaivor } from ".";

describe.only("test-fail", () => {
    test("test1", () => {
        const date = "1/26/2016 21:30";
        const result = failingBehaivor(date, "the end of next year");
        expect(`${result}`).not.toBe("Invalid Date");
    });

    test("test2", () => {
        const date = "2020-11-04T05:00:00.000Z";
        const result = failingBehaivor(date, "the end of the year 2022");
        expect(`${result}`).not.toBe("Invalid Date");
    });
});

when running the tests separately the console.log("parsedComparisonDate:", parsedComparisonDate); prints a correct value. however when running both tests the console.log("parsedComparisonDate:", parsedComparisonDate); prints Invalid Date.

Am I doing something wrong?

Den368 commented 3 years ago

however, I noticed that if I put the Sugar.Date.getLocale("en").addFormat("the {edge} of the year {yyyy}"); code inside the function it works.

shouldn't the "addFormat" add the format to the global Sugar module?

Den368 commented 3 years ago

found something intresting:

    // @ts-ignore
    console.log("Sugar.Date.getLocale():", DateUtils.Date.getLocale().compiledFormats);

output for the first run: the formats that been added via ".addFormat" are at the top of the ".compiledFormats" array. but in the second test the format regex moved to third place, and for the sentence "the end of the year 2022" it catches the first default format:

Sugar.Date.getLocale(): [
      {
        reg: /^ *(?:the)? ?(first day|first|beginning|last day|end|last) ?(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tue|Wed|Thu|Fri|Sat|weekend)? ?(?:of|in) ?(last|the|this|next)? ?(day|week|month|year|days|weeks|months|years)? ?(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sept|Oct|Nov|Dec|Sep)?,? ?([-−+]?\d{4,6}|'\d{2})? *$/i,
        to: [ 'edge', 'weekday', 'shift', 'unit', 'month', 'year' ]
      },

image

Found the bug:

Sugar.Date.getLocale().addFormat("the {edge} {weekday} of {shift} {unit} {month} {year}"); isn't working well. by the regex it should catch the date. however it's not.

combination of {unit} {year} in the format fails the format