lundi 3 juin 2019

Mock moment() in jest

I try to mock moment() to avoid that a snapshot-test fails based on time of day.. My <Header /> has a function that uses moment() to show different greetings (Hello, Good morning, good evening etc)

My test function:

jest.mock('moment', () => moment().month(11).date(24)); //Should give "happy xmas"
it("Match snapshop", () => {
    act(() => {
        container = shallow(<Header />);
    });
    expect(container).toMatchSnapshot();
});

But when I run the test I get:

ReferenceError: moment_1 is not defined

If I remove jest.mock(....) the test runs, but results depends on time of day..

Aucun commentaire:

Enregistrer un commentaire