Allow for karma.conf.ts when using ng test
Karma can read .ts files for its configuration, it'd be really great if ng test would support that too.
Perhaps the same can be done for protractor.conf.ts.
This can be achieved by editing .angular-cli.json
"test": {
"karma": {
"config": "./karma.conf.ts"
}
},
The CLI will then accept the karma.conf.ts file
Here's an example karma.conf.ts (using Mocha instead of Jasmine)
import * as karma from 'karma';
module.exports = function (config: karma.Config) {
// This extra variable is required because some configuration values (like 'mime')
// aren't registered in karma.Config and Typescript will complain about it
const configuration: any = {
basePath: '',
frameworks: ['mocha', '@angular/cli'],
plugins: [
require('karma-mocha'),
require('karma-mocha-reporter'),
require('karma-chrome-launcher'),
require('karma-coverage-istanbul-reporter'),
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: {
environment: 'dev'
},
reporters: config['angularCli'] && config['angularCli'].codeCoverage
? ['coverage-istanbul', 'mocha']
: ['mocha'],
mochaReporter: {
showDiff: true
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
singleRun: false
browsers: ['Chrome'],
};
config.set(configuration);
};
Can there by an option to NOT specify a karma.conf file? Perhaps working out of the box by default? Now I get "Path must be a string. Received undefined"
It sounds like you didn't surround the value with quote marks. What does your entry in .angular-cli.json look like?
@jensbodal I didn't have an angular-cli.json, I had to copy it from another project. Pretty poor message from a framework because of a missing file.
i followed the instructions in corverting karma.conf.js > karma.conf.ts. also updated .angular-cli.json to use the new file, and installed the required dependencies and i got the following error.
C:\Users\eskin\Desktop\Angular2>ng test
10% building modules 1/1 modules 0 active29 06 2017 14:52:56.704:WARN [proxy]: proxy "/base/src\assets" normalized to "/base/src\assets/"
29 06 2017 14:53:00.777:WARN [karma]: No captured browser, open http://localhost:9876/
29 06 2017 14:53:00.788:INFO [karma]: Karma v1.4.1 server started at http://0.0.0.0:9876/
29 06 2017 14:53:00.789:INFO [launcher]: Launching browser Chrome with unlimited concurrency
29 06 2017 14:53:00.799:INFO [launcher]: Starting browser Chrome
29 06 2017 14:53:20.351:WARN [karma]: No captured browser, open http://localhost:9876/
29 06 2017 14:53:20.978:INFO [Chrome 59.0.3071 (Windows 10 0.0.0)]: Connected on socket eibLXhkdGkir65eQAAAA with id 71406458
START:
Chrome 59.0.3071 (Windows 10 0.0.0) ERROR
Uncaught Error: Missing: jasmine.js
at http://localhost:9876/_karma_webpack_/vendor.bundle.js:36663
Finished in 1.135 secs / 0 secs @ 14:53:22 GMT-0400 (Eastern Daylight Time)
SUMMARY:
√ 0 tests completed
any thoughts @Ionaru :/
i was able to solve the solution, since i was using jasmine for my tests, i forgot to add it.
I am getting the below error when trying to use karma.config.ts with angular cli:
20 03 2018 13:56:22.699:ERROR [config]: Invalid config file!
*
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (module.js:613:28)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
at Object.parseConfig (
typescript ts-node are already installed in this project. I verified this in node_modules as well and they are present.
What am i doing wrong? cc @Ionaru
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.