marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mockDeep nested object issue

jpasquers opened this issue · comments

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?

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});