Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Class constructor cannot be invoked without 'new' #571

Closed
icfantv opened this issue Jun 6, 2018 · 6 comments
Closed

TypeError: Class constructor cannot be invoked without 'new' #571

icfantv opened this issue Jun 6, 2018 · 6 comments

Comments

@icfantv
Copy link

icfantv commented Jun 6, 2018

  • Issue

  • Expected behavior

    • Tests should execute normally, without error.
  • Output from your debug log

    • This file is not being generated
  • Link to a minimal repo that reproduces this issue

  • Optional (but highly recommended) - Configure Travis (or your favorite system) with the minimal repo

    • Not strictly necessary. Can be easily run/seen from command line or from within IDE.
@icfantv
Copy link
Author

icfantv commented Jun 6, 2018

I can reproduce this issue with https://github.com/kentcdodds/react-testing-library as well. What I don't understand, however, is why it looks like ts-jest is never getting invoked. I set a breakpoint in the ts-jest library code and it's never getting hit so I may be doing something wrong there.

@icfantv
Copy link
Author

icfantv commented Jun 6, 2018

Another update: I tried with core-decorators instead of lodash-decorators and the tests pass just fine.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 3, 2018

Hi,

I used to debug a bit with ts-jest and I found out Jest cached all the executed codes after the first run. Therefore the breakpoint won't hit anymore after the first run.

Nevertheless, have you tried to put console log or breakpoint at transpiler.ts ? There is a method transpileTypeScript in it which always triggers (ofc if you don't use Babel). I haven't tried to debug your codes yet but I think I should share this about debugging ts-jest.

Side information: have you tried with ts-jest v23 ? Do you encounter the same issue ?

@icfantv
Copy link
Author

icfantv commented Jul 3, 2018

Apologies for not following up on this. The issue was with the way lodash-decorators was operating on class constructors. It was doing it in a way that the compiler couldn't handle. I’d filed an issue with the LD dev and he has fixed. I would recommend against using core decorators because of the way it implements the bind operation in that the author chose to do this via a getter meaning the function that applies the bind is executed every time and it doesn’t need to be.

@anuraghazra
Copy link

Hi, i'm also facing a similar issue with this, I know the reason behind it (https://stackoverflow.com/questions/51860043/javascript-es6-typeerror-class-constructor-client-cannot-be-invoked-without-ne) but i could not make it work. any ideas?

Currently i'm trying to test a slider component which mocks the MouseEvent like so

beforeAll(() => {
    const oldMouseEvent = MouseEvent;
    // @ts-ignore
    global.MouseEvent = class FakeMouseEvent extends MouseEvent {
      _init: { pageX: number; pageY: number };
      constructor(name: any, init: any) {
        super(name, init);
        this._init = init;
      }
      get pageX() {
        return this._init.pageX;
      }
      get pageY() {
        return this._init.pageY;
      }
    };
    // @ts-ignore
    global.MouseEvent.oldMouseEvent = oldMouseEvent;
  });
  afterAll(() => {
    // @ts-ignore
    global.MouseEvent = global.MouseEvent.oldMouseEvent;
  });

image

@icfantv
Copy link
Author

icfantv commented Oct 30, 2020

@anuraghazra I would encourage you to post your issue on SO along with what you've already tried w/r/t debugging as your request does not initially appear to be related to the original issue (which turned out to not be the result of the ts-jest library).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants