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

Break sinon.spy(Component.prototype, 'method')

Open noru opened this issue 7 years ago • 8 comments

When using '@autobind' on a component, then in tests sinon.spy(Component.prototype, 'someMethod') will yield an error:

TypeError: Attempted to wrap undefined property undefined as function
...

Sorry, currently I can't provide more detail, but I'm sure @autobind is the only variable between success/error in tests.

noru avatar May 07 '17 16:05 noru

+1 same problem with jest


        const instance = component.instance();
        const spy = jest.spyOn(Component.prototype, 'compute');

        component.find('button').simulate('click');
        component.update();

        expect(spy).toBeCalled();

damianobarbati avatar May 11 '17 23:05 damianobarbati

98968ee might fixed this. Please try the latest master branch

stevemao avatar Jul 01 '17 04:07 stevemao

Please comment if doesn't work for you

stevemao avatar Dec 18 '17 23:12 stevemao

Just in case anyone else happens upon this, I'm fairly certain autobind-decorator is incompatible with sinon (at least with .stub()). It appears sinon uses descriptor.value to check if something is a method or not: https://github.com/sinonjs/sinon/blob/v4.3.0/lib/sinon/stub.js#L30-L34.

As a result, the descriptor.get() that autobind-decorator creates here: https://github.com/andreypopp/autobind-decorator/blob/v2.1.0/src/index.js#L75-L95 and here: https://github.com/andreypopp/autobind-decorator/blob/v2.1.0/src/index.js#L83-L92

are always considered a non-function properties.

I didn't dig into sinon.spy as much, but I don't think it was working on @autobind methods either.

job13er avatar Feb 15 '18 19:02 job13er

@job13er just declare your class methods as class properties.

damianobarbati avatar Feb 15 '18 19:02 damianobarbati

@damianobarbati Thanks. That's always an option, but since I was looking at autobind-decorator, it seems obvious I already have reasons not to want to use class properties, or I would have had no need for autobind-decorator in the first place.

EDIT: Your comment did make me take a second look at why we weren't using class properties in the first place though, so thank you :)

job13er avatar Feb 15 '18 20:02 job13er

@job13er Maybe we should add this to the docs

stevemao avatar Feb 15 '18 22:02 stevemao

Was running into sinon stubs clashing with autobind as well - is there a chance to reopen this issue or is it considered WONTFIX?

stoically avatar Jul 24 '19 04:07 stoically