Mock a namespace and a function with same name using Jest Mock a namespace and a function with same name using Jest typescript typescript

Mock a namespace and a function with same name using Jest


You can define a custom mock for your module in a __mocks__ folder in your project root (defined in jest config!). So you can create __mocks__/foo.js with mocked foo module functions and Jest automatically loads this module for test purposes. Also you can specify custom path to your mock:

jest.mock('foo', () => jest.requireActual('../some/another/folder/foo.js'));

Check a Jest manual for more info


If 'foo' is under global, you may try adding codes below in the beginning of test script. Or write them in a .js file in and import it in the first line.

Object.defineProperty(global, "foo", {    value: {        bar: {            baz: ()=>{                 return true;               },            },        },    },});

Or simply

global["foo"] = {    bar: {        baz: () => {            return true;        },    },};

to mock one.


According to the docs using a dot inside of a name is fine, but I have a gut feeling against it.

I recommend you to nest the namespaces.

You can include the declaration of bar into foo

declare namespace foo {  function bar();  declare namespace bar {    function baz();  };};

Proof of concept

foo.ts

export namespace foo {    export function bar() { return "bar"}    export namespace bar {        function baz() { return "baz"}    }}

foo.test.ts

import {foo} from "./foo"describe("jest-mock-namespace", () => {    it("Should mock foo.bar to return whatever we want", () => {        const WWW = "WHATEVER_WE_WANT";        // @ts-ignore        foo.bar = jest.fn().mockImplementationOnce( () => WWW);        const result = foo.bar();        expect(result).toStrictEqual(WWW)    });    it("Should mock foo.bar.baz to return whatever we want", () => {        const WWW = "WHATEVER_WE_WANT";        // @ts-ignore        foo.bar.baz = jest.fn().mockImplementationOnce( () => WWW);        // @ts-ignore        const result = foo.bar.baz();        expect(result).toStrictEqual(WWW)    });})