Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Den368 opened this issue Jan 7, 2021 · 2 comments
Open

Comments

@Den368
Copy link

Den368 commented Jan 7, 2021

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
Copy link
Author

Den368 commented Jan 7, 2021

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
Copy link
Author

Den368 commented Jan 10, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant