Unit Tests are failing after updating to Angular 13

Viewed 1468

After updating to Angular 13, lots of my unit tests, that were running without problems on Angular 12, are now failing. What I found more frustrating is that the tests are failing only when running together, but not when I isolate them with fdescribe.

I've read that in Angular 13, the teardown options is set to true per default, so I tried to opt out globally in my test.ts file:

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(), {
    teardown: { destroyAfterEach: false }
  }
);

But this didn't work.

My Karma config:

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-junit-reporter'),
      require('karma-coverage'),
      require('karma-mocha-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    client: {
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
      jasmine: {
        random: false,
      },
    },
    coverageReporter: {
      dir: require('path').join(__dirname, '../../coverage/cockpit'),
      reporters: [
        { type: 'html', subdir: 'report-html' },
        { type: 'lcov', subdir: 'report-lcov' },
        { type: 'text-summary' },
      ],
      fixWebpackSourcePaths: true,
    },
    junitReporter: {
      outputDir: require('path').join(__dirname, '../../junit-report/cockpit'),
      suite: 'cockpit',
      outputFile: 'cockpit-tests-report.xml',
      fixWebpackSourcePaths: true,
    },
    reporters: ['mocha', 'kjhtml', 'coverage', 'junit'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true,
  });
};

In the browser console I see lots of the following error:

NG0303: Can't bind to 'ngTemplateOutlet' since it isn't a known property of 'ng-container'

I'm running out of ideas.

3 Answers

You didn't post a copy of your test(s), but I think you might need to import the Angular CommonModule inside it, like this:

import { CommonModule } from '@angular/common';
beforeEach(() => {
  TestBed.configureTestingModule({
    imports: [
      CommonModule
    ]
  }).compileComponents();
}));

The problem was caused by the ngrx store. It is now required to reset the selectors in the afterEach block

afterEach(() => {
    store.resetSelectors();
    fixture.destroy();
    TestBed.resetTestingModule();
});

what works for me was, to continue using the karma-coverage-istanbul-reporter

My Karma config:

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-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-sonarqube-unit-reporter'),
      require('karma-mocha-reporter')
    ],
    client: {
      clearContext: false
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, './coverage/frontend'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    sonarQubeUnitReporter: {
      sonarQubeVersion: 'LATEST',
      outputFile: 'reports/ut_report.xml',
      overrideTestDescription: true,
      testPaths: ['src'],
      testFilePattern: '.spec.ts',
      useBrowserName: false,
      prependTestFileName: 'frontend'
    },
    reporters: ['mocha', 'kjhtml', 'coverage-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'], 
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },
    singleRun: false,
    restartOnFileChange: true,
   
  });
};

Related