1

I want to run unit tests in typescript. I have a simple folder structure where directory app contains an app.ts like following

export module app {
  export class Config {
    testMe() {
      return "Hallo";
    }
  }
}

The unit tests that lies in directory test:

import app = require('../app/conf');
import * as chai from 'chai';

var config = new app.app.Config();
chai.expect(config.testMe()).to.equals("Hallo");

As TypeScripts documentation states in http://www.typescriptlang.org/Handbook#modules-pitfalls-of-modules when TypeScript code is used as an external module it does not make sense to use the module concept at all.

As you can see app.app.Config is not a very elegant way.

I can only run my unit tests on compiled TypeScript code. So I can only use modules if I don't care about unit tests or is there a simpler way?

rainerhahnekamp
  • 1,087
  • 12
  • 27

1 Answers1

1

Have app.ts:

export class Config {
    testMe() {
        return "Hallo";
    }
}

and test.ts:

import app = require('../app/conf');
import * as chai from 'chai';

var config = new app.Config();
chai.expect(config.testMe()).to.equals("Hallo");

And if your original code worked, so will this. No more app.app and no more needless internal modules.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thanks, I know that it does work that way. But my question was if I have to dismiss modules at all when doing unit tests in TypeScript or if there is another way, where I can keep the modules. – rainerhahnekamp Jan 04 '16 at 23:49
  • 1
    You can keep as many modules as you want. But why would you want to if you are using external modules anyways. – basarat Jan 04 '16 at 23:52
  • Can I run my unit tests without treating my app's codebase as external module? – rainerhahnekamp Jan 04 '16 at 23:54
  • 1
    Not easily. Have external modules all the way. Checkout webpack – basarat Jan 05 '16 at 00:25
  • Since I've marked your answer as solution can you please add a note that explicitely states that one should NOT use modules at all? Will be a direct answer to the original question. Thanks. – rainerhahnekamp Jan 08 '16 at 10:49