在TypeScript中将类从模块导入到mocha测试

时间:2018-02-27 11:26:17

标签: typescript mocha

我有一个文件src / core / version.ts

module MyModule.Core {
  /** Version information. */
  export class Version {
    /** The informal version. */
    public static getInformalVersion(): string {
      return "1.0 Beta 1";
    }    
  }
}
export default MyModule.Core.Version;

和test test / core / version.test.ts

import {assert} from 'chai';
import Version from "../../src/core/version";

describe('Version',()=>{
    it('getInformalVersionTest',()=> {
        let number =Version.getInformalVersion();
    assert.isNotNull(number);
    });
});

如果我运行mocha test --recursive

所有成功但咕噜失败

error TS1148: Cannot compile modules unless the '--module' flag is provided.

此字符串中的错误

 export default MyModule.Core.Version;

如果我清除它grunt编译项目但是mocha测试失败

TS23006 File './src/core/version.ts' is not a module

请告诉我如何将类版本导入测试版本.test.ts

1 个答案:

答案 0 :(得分:-1)

我找到了解决方案:

import {assert} from 'chai';
import * as fs from "fs";

var fileContent = fs.readFileSync("./src/core/version.js", "utf8");
if( fileContent.indexOf("exports.MyModule = MyModule;") === -1 )
    fs.appendFileSync('./src/core/version.js', "\n exports.MyModule = MyModule;");

import * as MyModule from "../../src/core/version.js";
import Version = MyModule.MyModule.Core.Version;
describe('Version',()=>{
    it('getInformalVersionTest',()=> {
        let number =Version.getInformalVersion();
    assert.isNotNull(number);
    });
});