Skip to content

New AOT option for Karma doesn't play nicely with TestBed.overrideComponent #30017

@hamfastgamgee

Description

@hamfastgamgee

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When running Karma in the new-to-19.2.x AOT mode, if you have any tests that use TestBed.overrideComponent() to specify different metadata to the component (an override to the providers property, for example), the test fails with an error about missing the @component decorator or having missing metadata. The error is thrown from invalidTypeError in test_bed_compiler.ts. As far as I could tell, the getAnnotation function in the resolver was returning null instead of Component, causing the error to be tripped. I assume the AOT build is stripping this metadata/the decorator before this check is made. I couldn't find any way to work around the issue other than rearchitecting my component so that I was no longer needing to make the overrideComponent call. As long as overrideComponent was in the stack at all, regardless of what parameters I passed to it, the tests failed due to missing metadata.

I have not tested any of the other types of overrides, but given that most of the code in the TestBed compiler is in common between the decorated types, I would assume they all are impacted in AOT mode.

Minimal Reproduction

Create a simple valid component, turn on AOT for Karma, and make a test where you use TestBed.overrideComponent to reset metadata. You don't even need to specify an actual override in the second parameter to trigger the issue: TestBed.overrideComponent(YourComponent, {}) triggers the failure.

Exception or Error

_<class name> class doesn't have @Component decorator or is missing metadata.

Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 19.2.6
Node: 22.14.0
Package Manager: npm 11.2.0
OS: win32 x64

Angular: 19.2.5
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.1902.6
@angular-devkit/build-angular     19.2.6
@angular-devkit/core              19.2.6 (cli-only)
@angular-devkit/schematics        19.2.6
@angular/cdk                      19.2.8
@angular/cli                      19.2.6
@angular/material                 19.2.8
@angular/material-luxon-adapter   19.2.8
@schematics/angular               19.2.6
rxjs                              7.8.2
typescript                        5.8.2
zone.js                           0.15.0

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions