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

问题描述 投票:0回答:1

我有一个文件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 / 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编译项目,但摩卡测试失败

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

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

typescript mocha
1个回答
-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);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.