rainerhahnekamp
rainerhahnekamp

Reputation: 1136

TypeScript UnitTests on Module

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?

Upvotes: 1

Views: 1185

Answers (1)

basarat
basarat

Reputation: 276171

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.

Upvotes: 1

Related Questions