jestjs/jest

[Bug]: Class constructor FormControl cannot be invoked without 'new'

GeorgeKnap opened this issue · 8 comments

Version

27.2.3

Steps to reproduce

here is the public repo with the issue

The issue is in the workspace jest-error-repo it uses NX workspace with Jest. Run the test of the library inside with nx test test-this-lib

in the native angular repo which uses Karma test passes.
I also put the issue in ng-stack/forms

Expected behavior

no error

Actual behavior

jest throws following error:

TypeError: Class constructor FormControl cannot be invoked without 'new'

       8 | })
       9 | export class TestThisComponentComponent {
    > 10 |   control = new FormControl<string>('init value');
         |             ^
      11 | }
      12 |

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
  Binaries:
    Node: 16.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.3.0 - C:\Program Files\nodejs\npm.CMD

after importing NgStackFormsModule in the test module the error changes to
https://user-images.githubusercontent.com/11920691/151941714-18c6dca3-076b-42aa-902d-f009cfab1e72.png

Neither of these errors are thrown by Jest, your issue probably lies with a transformer or custom wrapping code

@SimenB In there repo there are just files scaffolded during generating project, nothing custom, nothing special.

That might be, that still doesn't mean it's a bug in Jest. I've not used Angular since v1, so I have zero frame of reference to know what's going on, and I'm not gonna spend the time to try to understand how projects using it are structured. It's 99.999% certain the issue is due to a transformer though (Jest only runs JS, and since there is TS in your snippet, that means there's a transformer between the source code and the TypeError, which is a runtime error).

Just like projects created using create-react-app aren't minimal reproductions, ones created with Nx aren't. If you can strip away all the abstractions added on top of Jest I can take a look (so you're left with pretty much just the transformer and some setup code, maybe some other bits and bobs), but I assume that by the time you've done that you'll discover the source of the error yourself 🙂 https://github.com/GeorgeKnap/jest-ng-stack-forms-error/blob/f5daeddcca25b9d383f5d4fd25848c2f6f9a9f24/jest-error-repo/package.json#L13-L56 is not minimal even though a tool generated most of it

@SimenB I have created pure Angular 13 + Jest repo (taken mostly from jest-preset-angular example)
https://github.com/GeorgeKnap/angular-jest-ng-stack-forms
is it better repo to investigate?
the error that jest is throwing now is Unexpected value 'NgStackFormsModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

Better, definitely! That error is still an Angular specific one, though, not from Jest. I'd rip out the preset and as much typescript as you can.

Seeing as I don't think this is a bug in Jest, I'll close this issue. We recommend using StackOverflow or our discord channel for questions.

If after you've gotten an answer from those forums, you still think it's a bug in Jest, I'm happy to take a look. But I'm almost entirely certain your issue is a configuration errors. If with Jest, probably a transformer. I have no frame of reference to say if the @angular/testing stuff is used correctly. But in the end, all the errors you're getting is runtime errors when executing your code, which Jest has little to do with directly

After including ngcc in postinstall script as suggested here angular#35805 I'm back with the
Class constructor FormControl cannot be invoked without 'new'
image

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.