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

mockDeep nested object issue #82

Open jpasquers opened 2 years ago

jpasquers commented 2 years ago

I created a repository showcasing this:

https://github.com/jpasquers/jest-mock-extended-fail-example

The important part being the following

import { mockDeep } from "jest-mock-extended";

interface X {
    y: {
        [a: string]: string
    }
    z: {
        z1: string;
    }
}

describe('nested mock', () => {
    it('should be empty inner object', () => {
        let mock = mockDeep<X>({y: {}});
        //This fails -> Received: [Function mockConstructor]
        expect(mock.y["something"]).toBeUndefined();
    })
})

Since I am providing an implementation of y that is {} shouldn't mock.y["something"] be undefined?

adworacz commented 2 years ago

Just ran into this surprise as well.

Edit - using undefined instead of an empty object literal {} ended up working for me:

mockDeep<X>({y: undefined});