Home > database >  jest - mocking Date correctly when calling toHaveBeenCalledWith
jest - mocking Date correctly when calling toHaveBeenCalledWith

Time:11-25

I'm trying to unit test a service that creates a cookie from a response from an API I have created.

export interface ISessionService {
    createSession(): Observable<ApplicationSession>;
    validateSession(): Observable<boolean>;
}

The response looks something like this:

export abstract class ApplicationSession {
    public readonly reference: string;
    public readonly dateCreated: Date;
    public readonly expiryDate: Date;
}

When the SessionService.createSession() is called, it does an rxjs tap and creates a cookie via another service, I'm trying to test that the cookieService is called with the correct parameters. Like so:

 describe('given a successful request to create a session', () => {
        beforeEach(() => {
            jestSpyOn(cookiesService, 'setCookie').mockClear();
            jestSpyOn(sessionApi, 'createSession').mockReturnValue(of({
                data: {
                    sessionReference: 'some-reference',
                    dateCreated: '1996-10-15T04:35:32.000Z',
                    expiryDate: '1996-10-15T15:35:32.000Z',
                    statusCode: 200
                },
                exception: null,
                hasError: false
             }));

        });

        it('Then a session cookie is set from the API response', (done) => {
            subject.createSession().subscribe();
            expect(cookiesService.setCookie).toHaveBeenCalledWith(ApplicationCookies.SESSION_COOKIE, {
                dateCreated:'1996-10-15T04:35:32.000Z',
                expiryDate: '1996-10-15T15:35:32.000Z',
                reference: 'some-reference'
            }, { expires: '1996-10-15T15:35:32.000Z', path: "/", sameSite: "strict", secure: true });

            done();
        });
});

I always seem to get the same error though:

Error: expect(jest.fn()).toHaveBeenCalledWith(...expected)

- Expected
  Received

  "mock-sessionCookie",
  Object {
-   "dateCreated": "1996-10-15T04:35:32.000Z",
-   "expiryDate": "1996-10-15T15:35:32.000Z",
    "dateCreated": 1996-10-15T04:35:32.000Z,
    "expiryDate": 1996-10-15T15:35:32.000Z,
    "reference": "some-reference",
  },
  Object {
-   "expires": "1996-10-15T15:35:32.000Z",
    "expires": 1996-10-15T15:35:32.000Z,
    "path": "/",
    "sameSite": "strict",
    "secure": true,
  },

Number of calls: 1

I've tried using date.parse('') but that doesn't work... What is the correct way to make this assertion? I can't figure it out. I can't just put the date in like the test suggests as it's not a number.

Thanks

CodePudding user response:

This problem is likely occurring because your call to mockReturnValue is returning a string for a date instead of a Date object.

Hence try changing your mockReturnValue calls to return Date objects instead of strings e.g.

dateCreated: new Date('1996-10-15T04:35:32.000Z'),
expiryDate: new Date('1996-10-15T15:35:32.000Z'),
  • Related