marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
828 stars 57 forks source link

.then defined as a property on an object cannot be used for mock implementations #55

Closed PBug90 closed 3 years ago

PBug90 commented 3 years ago

This breaks older APIs that might chain a .then() call, like the following example using jsforce:

Minimum test setup example:

const batchMock = mock<jsforce.Batch>();
batchMock.then.mockResolvedValue(123)
// TypeError: Cannot read property 'mockResolvedValue' of undefined

Batch source code with .then defined on Batch prototype: https://jsforce.github.io/jsforce/doc/api_bulk.js.html#line486

The reason is the following line: https://github.com/marchaos/jest-mock-extended/blob/d08e7811dafe7b42da8513a6ed284a3d80f63998/src/Mock.ts#L91

marchaos commented 3 years ago

You should be able to remove the "then" prop for a test using

JestMockExtended.configure({
    ignoreProps: [],
})

in your afterEach() you can do

JestMockExtended.resetConfig();

You need version 2.0.1 to do this