autobind-decorator icon indicating copy to clipboard operation
autobind-decorator copied to clipboard

Using along with Jest

Open budziam opened this issue 4 years ago • 4 comments

Decorating the method with @boundMethod decorator causes that Jest creates incorrect mocked object based on class definition.

Example code

import { boundMethod } from "autobind-decorator";
import jestMock from "jest-mock";

class Abc {
    @boundMethod
    foo() {
        //
    }

    bar() {
        //
    }
}

const Mock = jestMock.generateFromMetadata(jestMock.getMetadata(Abc));
const obj = new Mock();
console.log(Object.keys(obj)); // === ["bar"]

The problem is that the object obj doesn't have the foo method.


The way how to create mocked object instance is taken from this library: https://www.npmjs.com/package/jest-create-mock-instance

budziam avatar Oct 18 '19 06:10 budziam

Does #79 or #57 work for you?

stevemao avatar Oct 18 '19 07:10 stevemao

None of those solve my issue

budziam avatar Oct 18 '19 08:10 budziam

Still an issue as of today: when using something like

jest.spyOn(instance, "myHandler");

Where myHandler is a boundMethod in my class component that I'm trying to test, test gives the following error:

TypeError: object[methodName].mockImplementation is not a function

cfecherolle avatar Jul 09 '21 11:07 cfecherolle

Any updates on this? Has anyone solved this?

tibabit avatar Oct 06 '22 12:10 tibabit