angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Allow for karma.conf.ts when using ng test

Open Ionaru opened this issue 9 years ago • 8 comments

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.

Ionaru avatar Feb 18 '17 16:02 Ionaru

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);
};

Ionaru avatar Mar 03 '17 12:03 Ionaru

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"

ghost avatar Jun 12 '17 17:06 ghost

It sounds like you didn't surround the value with quote marks. What does your entry in .angular-cli.json look like?

jensbodal avatar Jun 20 '17 17:06 jensbodal

@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.

ghost avatar Jun 20 '17 17:06 ghost

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 :/

eskinderg avatar Jun 29 '17 19:06 eskinderg

i was able to solve the solution, since i was using jasmine for my tests, i forgot to add it.

eskinderg avatar Jun 29 '17 20:06 eskinderg

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! */src/tests/karma.prod.config.ts:8 import { * } from Something> ; ^^^^^^

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 (/node_modules/karma/lib/config.js:370:22) at new Server (/node_modules/karma/lib/server.js:79:20) at Promise (/node_modules/@angular/cli/tasks/test.js:45:33) at new Promise () at Class.run (/node_modules/@angular/cli/tasks/test.js:23:16) at Class.run (*/node_modules/@angular/cli/commands/test.js:122:25) 20 03 2018 13:56:22.701:ERROR [config]: You need to install TypeScript. npm install typescript ts-node --save-dev

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

codeNoobie avatar Mar 20 '18 21:03 codeNoobie

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.