Karma Jasmine unit test issue in angular 7 - cannot read property 'success' of undefined

Viewed 2589

I am having problem in running ng test using Karma and Jasmine. It stops the browser and when I cancel the run then it throws the below error.

> Cannot read property 'success' of undefined TypeError: Cannot read
> property 'success' of undefined
>     at TestCommand.runSingleTarget (C:\workspace\uif_mdm\node_modules\@angular\cli\models\packages\angular\cli\models\architect-command.ts:242:21)
>     at process._tickCallback (internal/process/next_tick.js:68:7)
2 Answers

This error was gone when I uninstalled karma-phantomjs-launcher. Also had to remove browsers: ['PhantomJs'] in karma.conf.js and add below instead

 browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
      base: 'ChromeHeadless',
      flags: ['--no-sandbox']
    },
  } 

When a ran the unit test with ng test this error was thrown. When a ran the unit test with npm run test (test script being ng test --watch=false --code-coverage) it ran successfully.

Adding flag --watch=false on ng test fixed for me.

Related