Skip to content

Latest commit

 

History

History
61 lines (56 loc) · 1.77 KB

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

File metadata and controls

61 lines (56 loc) · 1.77 KB
title public tags date
Don't use arrow function inside mockImplementation with this (Jest)
true
testing
jest
javascript
2021-04-15

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' }
    }
*/