Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ng test throws an error Cannot find module 'angular-cli/plugins/karma'

Tags:

angular

Using angular-cli trying to write a test for a service after executing ng test. I get this error:

02 03 2017 14:51:09.486:ERROR [config]: Error in config file!
 { Error: Cannot find module 'angular-cli/plugins/karma'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at module.exports (/Desktop/e2-frontend/karma.conf.js:12:7)
    at Object.parseConfig (/Desktop/e2-frontend/node_modules/karma/lib/config.js:342:5)
    at new Server (/Desktop/e2-frontend/node_modules/karma/lib/server.js:56:20)
    at Promise (/Desktop/e2-frontend/node_modules/@angular/cli/tasks/test.js:34:33)
    at Class.run (/Desktop/e2-frontend/node_modules/@angular/cli/tasks/test.js:15:16)
    at Class.run (/Desktop/e2-frontend/node_modules/@angular/cli/commands/test.js:106:25)
    at Class.<anonymous> (/Desktop/e2-frontend/node_modules/@angular/cli/ember-cli/lib/models/command.js:134:17)
    at process._tickCallback (internal/process/next_tick.js:103:7) code: 'MODULE_NOT_FOUND' }

karma.config.js

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

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'angular-cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma')
    ],
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['angular-cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    remapIstanbulReporter: {
      reports: {
        html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'karma-remap-istanbul']
              : ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

It seems like it cannot find karma for some reason. there has to be a different location for it. How can I fix this error?

like image 852
fatah Avatar asked Mar 02 '17 23:03

fatah


4 Answers

Replace this line


frameworks: ['jasmine', 'angular-cli'],  

with this

frameworks: ['jasmine', '@angular/cli'],

This line

require('angular-cli/plugins/karma')

to this

require('@angular/cli/plugins/karma')

This

preprocessors: {
  ./src/test.ts': ['angular-cli']
},

to this

preprocessors: {
  './src/test.ts': ['@angular/cli']
},

in your karma.config.js file. Hope this will help you.

like image 104
Harshit Avatar answered Nov 18 '22 01:11

Harshit


First, make sure you install the latest angular CLI (npm install --save-dev @angular/cli:latest) and remove 'angular-cli' in package.json

Second, go to karma.conf.js edit the following:

from frameworks: ['jasmine', 'angular-cli']

to frameworks: ['jasmine', @angular/cli']

from

require('angular-cli/plugins/karma') 

to

require('@angular/cli/plugins/karma')

from

preprocessors: {
      './src/test.ts': ['angular-cli']
    },

to

preprocessors: {
      './src/test.ts': ['@angular/cli']
    },

Then, edit your angular-cli.json

from

"environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }

to

"environmentSource": "environments/environments.ts",
      "environments": {
        "dev": "environments/environments.ts",
        "prod": "environments/environments.prod.ts"
      } 
like image 44
Jon Myclo Faustino Avatar answered Nov 18 '22 00:11

Jon Myclo Faustino


Working solution

Open your karma.conf.js file and replace every angular-cli/plugins/karma with:

@angular/cli/plugins/karma

The mentioned properties to replace are inside following keys:

  • frameworks,

  • plugins,

  • preprocessors.

Then, the modified properties should look like:

frameworks: ['jasmine', '@angular/cli'],
plugins: [
   require('karma-jasmine'),
   require('karma-chrome-launcher'),
   require('karma-remap-istanbul'),        
   require('@angular/cli/plugins/karma')
],
preprocessors: {
  './src/test.ts': ['@angular/cli']
}
like image 2
kind user Avatar answered Nov 18 '22 01:11

kind user


Check your package.json and make sure you don't have a line for "angular-cli". If you do, remove it and run: npm install. This cleared it up for me. There was a reference for this in the dependencies and for "@angular/cli". "@angular/cli" is the new location for the angular cli.

like image 1
Adam Avatar answered Nov 18 '22 01:11

Adam