Don't use arrow function inside mockImplementation with this (Jest)

When using the arrow function:

// ...
      someAPI.JWT = jest.fn().mockImplementation(() => {
        this.authorize = jest.fn();
        this.credentials = { access_token: "fake_access_token" };
      });
// ...  
const jwtInstance = someAPI.JWT.mock.instances[0];
console.log(jwtInstance); // mockConstructor

When using function:

// ...
      someAPI.JWT = jest.fn().mockImplementation(function () {
        this.authorize = jest.fn();
        this.credentials = { access_token: "fake_access_token" };
      });
// ...
const jwtInstance = someAPI.JWT.mock.instances[0];
console.log(jwtInstance);
/*
    mockConstructor {
      authorize: [Function: mockConstructor] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      },
      credentials: { access_token: 'fake_access_token' }
    }
*/
Share:

About

My name is Max, also known as @mxkvl.

I am a software developer, currently working mostly with TypeScript, React and Node ecosystems.

I write a blog about topics that interest me most: programming languages in general, TypeScript, testing, writing and other great stuff.

Join the Mailing List