使用Jasmine进行TypeScript模块测试



我有以下结构:SRC文件夹中有file1.tsSpec文件夹(与SRC同级)包含file1.spec.ts我尝试运行typescript Jasmine测试file1。它看起来像这样:

export module moduleName {
export class className {
public doSomething(a: string): string {
return a;
}
}
}

file1.spec。它看起来像这样:

const aliasFile = require('../src/file1');
describe("tests", function () {
let f;
beforeEach(function () {
f = new aliasFile.className();
});
it("should run", function () {
const result = f.doSomething('aaa');
expect(result).toEqual('aaa');
});
});

当我运行这个测试时,我得到了这个结果:

  1. 测试应该运行
  • TypeError: aliasFile。className不是构造函数
  • TypeError: Cannot read property 'doSomething' of undefined

定义测试的正确方法是什么?

您可以尝试将require方法替换为别名import关键字,如:

import { moduleName as aliasFile } from './file1';
describe("tests", function () {
let f: aliasFile.className;
beforeEach(function () {
f = new aliasFile.className();
});
it("should run", function () {
const result = f.doSomething('aaa');
expect(result).toEqual('aaa');
});
});

最新更新