You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to write a test for a utility function in our project, which uses the formatDuration module from date-fns/formatDuration. Can anyone help me creating a mock for this module with Jest and be able to test if formatDuration was called with expected arguments?
This is my current setup:
// ../utilites/date.tsimportformatDurationfrom"date-fns/formatDuration";importsubSecondsfrom"date-fns/subSeconds";importintervalToDurationfrom"date-fns/intervalToDuration";exportconstcreateDurationFromSeconds=(seconds: number)=>{constend=newDate();conststart=subSeconds(end,seconds);returnintervalToDuration({ start, end });};typeFunctionArgs=Parameters<typeofformatDuration>;exportconstformatDurationHelper=(duration: FunctionArgs[0],options?: FunctionArgs[1])=>{//...do something with options.formatreturnformatDuration(duration,options);};
// ../utilites/date.test.tsimportformatDurationfrom"date-fns/formatDuration";import{formatDurationHelper,createDurationFromSeconds}from"./date";jest.mock("date-fns/formatDuration",()=>{return{__esModule: true,default: jest.fn()};});describe("formatDurationWithLocale",()=>{it("created mocked formatDuration",()=>{expect(jest.isMockFunction(formatDuration)).toBe(true);// <- this test is successful});it("does something with options.format",()=>{constduration=createDurationFromSeconds(1);formatDurationHelper(duration,{format: ["seconds"]});expect(formatDuration).toHaveBeenCalled();// <- this test is unsuccessfulexpect(formatDuration).toHaveBeenCalledWith(duration,{// <- this test is unsuccessfulformat: ["seconds"]});});});
expect(jest.isMockFunction(formatDuration)).toBe(true) is successful, but the others fail...
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi there!
I'm trying to write a test for a utility function in our project, which uses the
formatDuration
module fromdate-fns/formatDuration
. Can anyone help me creating a mock for this module with Jest and be able to test if formatDuration was called with expected arguments?This is my current setup:
expect(jest.isMockFunction(formatDuration)).toBe(true)
is successful, but the others fail...Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions