Trouble integrating Karma Typescript tests with NodeJS.

I'm playing around with Typescript and NodeJS and I created a card game service in TS. I wrote Karma tests to test my code and the tests pass and they're easy to debug. I'm happy. Then when I go to integrate with NodeJS I find out that in order to "require" the classes (i.e. var something = require('something_module_path')) I need to add a single line to the end of my TS files: export = something_module. My issue is that this breaks my Karma tests (Uncaught ReferenceError: module is not defined): the TS line export = something_module gets compiled to JS module.exports = something_module.
I've been stuck on this issue for quite a while: I can get Node to run but I can no longer run my Karma tests or I can run my Karma tests but Node doesn't function because the modules aren't exported. I'm wondering if anybody else has been in this situation and how can I remedy it so that I can integrate my TS files into NodeJS without breaking my Karma tests? I'm thinking that there must be a way to configure Karma to know about Node keywords but I'm stumped.

4 comments
Comment actions Permalink

Are you testing server-side code run by Node.js? Karma is not supposed to be used here then - it's designed for in-browser testing only.
see also http://stackoverflow.com/questions/16660670/how-to-test-nodejs-backend-code-with-karma-testacular.

I'd suggest using Mocha for server-side code testing

0
Comment actions Permalink

I want to follow your advice and try Mocha but I don't see it as a possible run configuration and I browsed the plugins and Mocha
is not there either. I'm using PHPStorm 10.3 build PS-143.1770

My tests are written using Jasmine, the OP says they're "Karma test" but it's really Jasmine tests run with a Karma run configuration.

0
Comment actions Permalink

There is no separate Mocha plugin - Mocha support is a part of NodeJS plugin; please make sure to install it.
To start with Mocha, just install mocha framework and create a Mocha run configuration. See https://www.jetbrains.com/webstorm/help/preparing-to-use-mocha-test-framework.html, https://www.jetbrains.com/webstorm/help/running-mocha-unit-tests.html#d193657e146

0
Comment actions Permalink

Thanks for your help, I was playing around with it more last night and was able to get it to work with a NodeJS
run configuration with the jasmine-node plugin. You lead me in the right direction by pointing out that I was testing server-side TS with a
client-side test runner. I can run my tests and debug them now.

0

Please sign in to leave a comment.