Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for karma.conf.ts (TypeScript) when using cli #1727

Open
tkrotoff opened this issue Nov 26, 2015 · 7 comments
Open

Allow for karma.conf.ts (TypeScript) when using cli #1727

tkrotoff opened this issue Nov 26, 2015 · 7 comments

Comments

@tkrotoff
Copy link

When running node_modules/.bin/karma start karma.conf.ts (1) (written in TypeScript) I get:

ERROR [config]: Invalid config file!
  SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.parseConfig (node_modules/karma/lib/config.js:276:22)
    at new Server (node_modules/karma/lib/server.js:56:20)
    at Object.exports.run (node_modules/karma/lib/cli.js:231:7)
    at Object.<anonymous> (node_modules/karma/bin/karma:3:23)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)

Also it does not seem to automatically detect karma.conf.ts when running node_modules/.bin/karma start:

[karma]: No captured browser, open http://localhost:9876/
INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
INFO [Chrome]: Connected on socket 9S-HxtcaP6-xFCRfAAAA with id manual-8425
Chrome ERROR
  You need to include some adapter that implements __karma__.start method!

On the other hand when running the task using gulp and the same karma.conf.ts:

// File gulpfile.ts (written in TypeScript)
import * as gulp from 'gulp';
import * as karma from 'karma';

function runKarma(done): void {
  const config = {
    configFile: __dirname + '/karma.conf.ts'
  };

  const server = new karma.Server(config, done);
  server.start();
}

gulp.task('test', done => runKarma(done));

Everything works as expected so I suspect code specific to the cli to not support TypeScript.

(1) or $(npm bin)/karma start karma.conf.ts

@dignifiedquire
Copy link
Member

I'm afraid Karma does not support TypeScript at all in configuration files. Not sure why it works when you use it through gulp (maybe some part of your build process handles it somehow, or more likely it is just processed by node with support for ES2015 instead of TypeScript, as your config files looks like it does not use anything that is actually TypeScript specific only ES2015 modules)

@tkrotoff
Copy link
Author

@dignifiedquire

it is just processed by node

I guess this is the answer. I've added some debug infos inside ts-node:

$ gulp test
[13:03:41] Requiring external module ts-node/register
DEBUG node_modules/ts-node/dist/ts-node.js compile(fileName), fileName: gulpfile.ts
[13:03:43] Using gulpfile gulpfile.ts
[13:03:43] Starting 'test'...
DEBUG node_modules/ts-node/dist/ts-node.js compile(fileName), fileName: karma.conf.ts
INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
[...]

As you can see, ts-node automatically compiles karma.conf.ts without me asking for it.

I like to write karma configuration file in TypeScript so I can use TypeScript definition for karma. It works nicely and if one day a karma parameter changes, I will be notified thx to the TypeScript definition.

// File karma.conf.ts (written in TypeScript)
/// <reference path="node_modules/DefinitelyTyped/karma/karma.d.ts" />

import * as karma from 'karma';

export = (config: karma.Config) => {
  config.set({
    // ...
  }):
};

Also TSLint does the linting on all my *.ts files including karma.conf.ts.

@NickClark
Copy link

I prefer this as well, mostly for consistency and tooling reasons.

@caseyhoward
Copy link

Even with this merged (#2224) I still can't figure out how to use ES6 modules. Is this supported. I get the following error:

11 07 2016 11:23:38.903:ERROR [config]: Invalid config file!
  SyntaxError: Unexpected reserved word

@aciccarello
Copy link

I noticed ngParty/Angular1-scaffold@db2a25d was running into this issue as well. If type annotations cannot be used it negates the benefit of TypeScript support.

// NOTE: we cannot use ES6 modules and type annotations yet, because we cannot configure ts-node that runs within karma

I think the ts-node register call needs to support some options

@aj-r
Copy link

aj-r commented Apr 24, 2018

This works for me using TypeScript 2:

npm install -D typescript ts-node karma @types/karma

In package.json:

{
  "scripts": {
    "test": "ts-node node_modules/karma/bin/karma start karma.conf.ts",
  },
}

Also don't forget to add this to your karma config file:

    mime: {
      "text/x-typescript": ["ts", "tsx"],
    },

Then run npm test

@bennycode
Copy link

The Karma Configuration documentation writes:

The Karma configuration file can be written in JavaScript, CoffeeScript, or TypeScript and is loaded as a regular Node.js module.

The documentation also lists ./karma.conf.ts as an example but when I try to run karma start with my karma.conf.ts file, then I am getting the following error:

ERROR [config]: Invalid config file!
TypeError: karma.conf.ts: Emit skipped
at getOutput (D:\dev\projects\wireapp\wire-web-packages\node_modules\ts-node\src\index.ts:337:15)
at Object.compile (D:\dev\projects\wireapp\wire-web-packages\node_modules\ts-node\src\index.ts:367:11)
at Module.m._compile (D:\dev\projects\wireapp\wire-web-packages\node_modules\ts-node\src\index.ts:413:43)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (D:\dev\projects\wireapp\wire-web-packages\node_modules\ts-node\src\index.ts:416:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants