[ZEPPELIN-6637] Compile Angular decorators in the shell unit test setup - #5435
Open
kimyenac wants to merge 1 commit into
Open
[ZEPPELIN-6637] Compile Angular decorators in the shell unit test setup#5435kimyenac wants to merge 1 commit into
kimyenac wants to merge 1 commit into
Conversation
The shell setup can run a spec that constructs a directive by hand, but not one that goes through TestBed. Three things are missing. Vitest 4 transforms specs with oxc, and that transform does not apply the decorator options the application build uses: they live in tsconfig.base.json, which src/tsconfig.json extends while excluding **/*.spec.ts. A decorated spec therefore fails to parse with "Invalid or unexpected token". Declaring the options on the vitest config makes the transform independent of that lookup. emitDecoratorMetadata then compiles to a __metadata helper that is a silent no-op unless Reflect.metadata exists, so constructor injection fails with NG0202. src/polyfills.ts pairs zone.js with core-js/es7/reflect for the application, and the setup already mirrors the first half, so mirroring the second needs no new dependency. Finally the setup never calls TestBed.initTestEnvironment(), and with vitest globals disabled Angular cannot install its per-test reset hook, so the second spec in a file hits "test module has already been instantiated". Adds a TestBed spec for ReactMountDirective as the first consumer, alongside the existing hand-driven one. It renders the directive from a host template, so the decorator metadata, the @input bindings and the constructor injection all have to resolve for it to run at all. It also provides zone change detection the way main.ts does, without which the zone assertions would pass vacuously against TestBed's zoneless default.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this PR for?
ZEPPELIN-6567 gave the Angular shell a vitest setup, but only for specs that build their subject by hand. The spec it added is named "mounts React remotes outside the Angular zone without TestBed" and constructs the directive with
new ReactMountDirective(...). A spec that goes through TestBed does not run yet, for three separate reasons.Vitest 4 transforms specs with oxc, and that transform does not apply the decorator options the application build uses. They live in
tsconfig.base.json, whichsrc/tsconfig.jsonextends while excluding**/*.spec.ts. A decorated spec therefore fails to parse withSyntaxError: Invalid or unexpected token. Declaringoxc.decoratoron the vitest config settles it independently of that lookup.emitDecoratorMetadatathen compiles to a__metadatahelper that is a silent no-op unlessReflect.metadataexists, so Angular JIT sees nodesign:paramtypesand constructor injection fails withNG0202.src/polyfills.tspairszone.jswithcore-js/es7/reflectfor the application, and the setup already mirrors the first half, so mirroring the second needs no new dependency.Finally the setup never calls
TestBed.initTestEnvironment(), and with vitest globals disabled Angular cannot install its per-test reset hook, so the second spec in a file hitsCannot configure the test module when the test module has already been instantiated.One note on the issue description. It attributes all of this to oxc not reading
experimentalDecoratorsandemitDecoratorMetadata, and suggests importing@angular/compiler. The parse failure is real, but the missing polyfill and the missing TestBed initialization are separate causes, and@angular/compilerturns out not to be needed:@angular/core/testingalready imports it, so the JIT facade is loaded either way.The TestBed spec for
ReactMountDirectiveis the first consumer, and sits alongside the hand-driven one rather than replacing it. It renders the directive from a host template, so the decorator metadata, the@Inputbindings and the constructor injection all have to resolve for it to run at all. It also provides zone change detection the waymain.tsdoes, without which the zone assertions would pass vacuously against TestBed's zoneless default.None of the three files ships in the application bundle, so there is no runtime change.
What type of PR is it?
Improvement
Todos
None
What is the Jira issue?
https://issues.apache.org/jira/browse/ZEPPELIN-6637
How should this be tested?
npm run test:shellfromzeppelin-web-angular, which is green at 5 specs across 2 files. Each piece of the setup was checked by removing it:oxcblock: the TestBed spec fails to parse,SyntaxError: Invalid or unexpected tokencore-js/es7/reflect: 3 failures,NG0202: This constructor is not compatible with Angular Dependency InjectioninitTestEnvironment:Need to call TestBed.initTestEnvironment() firstafterEach(resetTestingModule): the second spec onward fails withtest module has already been instantiatedrunOutsideAngulartorunmakes its zone assertion failNote that unit tests do not gate CI yet (ZEPPELIN-6566), so this suite has to be run locally for now.
Screenshots (if appropriate)
No, this changes test tooling only.
Questions: