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?
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.
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"
}
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']
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With