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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
You can keep as many modules as you want. But why would you want to if you are using external modules anyways.
Can I run my unit tests without treating my app's codebase as external module?
Not easily. Have external modules all the way. Checkout webpack
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.