Search code examples
angularkarma-jasmine

What could be causing 'ng test' to not execute any unit tests in my Angular project?


ng test

is not executing any test cases in spec files.

In my projeect, when I run

ng test

it is giving me the result as below:

seChrome 113.0.0.0 (Windows 10): Executed 0 of 0 SUCCESS (0.002 secs / 0 secs)
TOTAL: 0 SUCCESS

Test cases are written in the component level (spec files). The ng test command is not executing them.

I have looked into angular.json, tsconfig.spec.json, tsconfig.json and everything looks fine to me. I tried some things like downgradiung the target to ES2015 from ES 2022 but nothing seems to work.

Please suggest what might be the issue here. Thank you.

karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

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')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml', 'jasmine'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

Solution

  • I came across this link and tried the solution that worked for mylifeandcode.

    I then started encountering a different error as below: enter image description here

    Then I searched for the above error and came across this link and the accepted answer here worked for me.

    Replaced the contents of test.ts file with below worked:

    // This file is required by karma.conf.js and loads recursively all the .spec and framework files
    
    import "zone.js/testing";
    
    import { getTestBed } from "@angular/core/testing";
    import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";
    
    // First, initialize the Angular testing environment.
    getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());