Skip to content

Karma builder doesn't work with Angular 19.2.0+ #125

Closed
@alee-circunomics

Description

@alee-circunomics

We upgraded to Angular ^19.2.0 recently, and running unit tests with @ngx-env/builder:karma no longer works. We get errors like this:

...
Chrome Headless 134.0.0.0 (Linux x86_64) MyComponent when fn is called should trigger notification FAILED
	Failed: MyComponent class doesn't have @Component decorator or is missing metadata.
	    at invalidTypeError (node_modules/@angular/core/fesm2022/testing.mjs:1655:12)
	    at forEach (node_modules/@angular/core/fesm2022/testing.mjs:1079:23)
	    at Set.forEach (<anonymous>)
	    at TestBedCompiler.compileTypesSync (node_modules/@angular/core/fesm2022/testing.mjs:1071:32)
	    at node_modules/@angular/core/fesm2022/testing.mjs:994:40
	    at Generator.next (<anonymous>)
	    at asyncGeneratorStep (node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:3:1)
	    at apply (node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:17:1)
	    at _ZoneDelegate.invoke (node_modules/zone.js/fesm2015/zone.js:369:28)
	    at AsyncTestZoneSpec.onInvoke (node_modules/zone.js/fesm2015/zone-testing.js:892:39)
...

It worked with Angular 19.1.8 or earlier. Switching to @angular-devkit/build-angular:karma also works.

angular.json test

...
"test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": ["zone.js", "zone.js/testing"],
            "main": "src/test.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": [],
            "codeCoverageExclude": ["src/app/testing/**"]
          }
        }
...

test.ts

import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  [BrowserDynamicTestingModule, NoopAnimationsModule],
  platformBrowserDynamicTesting(),
  {
    teardown: { destroyAfterEach: false },
    errorOnUnknownElements: true,
    errorOnUnknownProperties: true,
  }
);

karma.config.js

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    client: {
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage'),
      reporters: [
        { type: 'html', subdir: 'report-html' },
        { type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
        { type: 'text-summary', subdir: '.' },
      ],
      fixWebpackSourcePaths: true,
      check: {
        emitWarning: false,
        global: {
          statements: 100,
          branches: 100,
          functions: 100,
          lines: 100,
          excludes: [],
        },
      },
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox', '--headless', '--disable-gpu', '--remote-debugging-port=9222'],
      },
      ChromiumHeadlessNoSandbox: {
        base: 'ChromiumHeadless',
        flags: ['--no-sandbox', '--headless', '--disable-gpu', '--remote-debugging-port=9222'],
      },
    },
    singleRun: false,
    restartOnFileChange: true,
  });
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions