jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

Angular jsonforms causes test failures with --code-coverage

Open Ketec opened this issue 2 years ago • 9 comments

Describe the bug

TypeError: Cannot read properties of undefined (reading '$state') It seems like Istanbul (karma code coverage) fails to detect the JsonFormsAngularService injections.

Potentially because it is not decorated as an injectable in the code?

This causes any component that uses to fail - manually providing JsonFormsAngularService in the test providers does not help.

Runs fine if coverage is disabled.

Expected behavior

Tests should run when using jsonforms.

Steps to reproduce the issue

USe </ in a component. Write unit test for the component (as long as it initializes.)

ng test --browsers=headless --watch=false --code-coverage

Screenshots

No response

In which browser are you experiencing the issue?

All

Framework

Angular

RendererSet

No response

Additional context

No response

Ketec avatar Feb 10 '22 08:02 Ketec

Hi @Ketec, this is very hard to diagnose for me without any further information. Note that we do code coverage tests in Angular Material just fine, using karma --coverage ourselves. Maybe you want to compare our settings to yours.

sdirix avatar Feb 10 '22 09:02 sdirix

I see it is using a rather old version of Karma. karma-coverage-istanbul-reporter I believe was deprecated in favour of karma-coverage. This was already with Angular 11.

Possibly also old compilation target: "target": "es5", Which was listed as a temporary workaround when I googled for similar coverage issues. But Angular 12 has already removed support for older browsers and new projects start with ES2015 as a minimum target.

ES2015 is also target in the seed app in https://github.com/eclipsesource/jsonforms-angular-seed (was going to try and replicate there ut its a bit out of date and does not compile).

Ketec avatar Feb 10 '22 11:02 Ketec

So would raising the target to es2015 in JSON Forms fix the problem for you? Seems weird that this would cause problems only for code coverage testing.

sdirix avatar Feb 10 '22 15:02 sdirix

Code coverage relies on istanbul which likely is not fully compatible with newer targets ES5 is the max target that works ES2015 no longer does.

Likely related to: https://github.com/angular/angular/issues/44781

Ketec avatar Feb 11 '22 16:02 Ketec

But ES5 breaks the newer library? So would your problems be fixed if we switched to ES2015 and karma coverage?

sdirix avatar Feb 14 '22 07:02 sdirix

I think the issue is more about the code itself JsonFormsAngularService is not decorated as an injectable service which likely leads to it not getting properly injected/detected by Istanbul when generating code coverage.

It works if using the old es5 target but starts failing if the target is increased to a newer version.

Ketec avatar Feb 14 '22 14:02 Ketec

Hi @Ketec, so all we need are some additional decorations? That's fine with me if they don't break anything. Would you be willing to contribute this?

sdirix avatar Feb 15 '22 07:02 sdirix

This may go deeper, I also see this is not an angular library but a custom webpack build?

Wanted to test increasing target to 2015 aswell for the angular adapter but that just leads to different errors ( Compiled class declaration is not inside an IIFE: JsonFormsBaseRenderer ).

Likely need to know more in-depth about specific build /project structure here.

Ketec avatar Feb 16 '22 07:02 Ketec

It's not really a custom webpack build but just invoking pure ngc with a config.

sdirix avatar Feb 28 '22 21:02 sdirix

I think this has been fixed in #2212 or is it still an issue ?

JBBianchi avatar Feb 27 '24 14:02 JBBianchi

@JBBianchi I think you are right: We are using target ES2021 by now and you updated the test runner. Closing this. We can repopen this or create a new issue if this problem re-appears. Thanks for the hint!

lucas-koehler avatar Feb 27 '24 16:02 lucas-koehler