我试图将TypeScript编译为单个JS文件。由于我需要它是可移植的(而不是在浏览器中运行),我需要使用命名空间而不是system
或amd
模块。
问题:无法运行测试。运行npm test
会导致:
src/foo.spec.ts (6,20): Cannot find namespace 'Test'.
这是我的简短示例程序:
tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": false,
"outFile": "test.js"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"src/**/*.spec.ts"
]
}
src/lib.ts
namespace Test {
export class Lib {
public hello(): string {
console.log("hello");
return "hello";
}
}
}
src/foo.ts
namespace Test {
// import Lib = Test.Lib; // < This doesn't work :(
export class Foo {
public sayHello(): string {
return new Test.Lib().hello();
}
}
}
new Test.Foo().sayHello();
src/foo.spec.ts
import {expect} from "chai";
import "mocha";
describe('Hello!', () => {
it('We should say hello!', () => {
const foo: Test.Foo = new Test.Foo();
expect(foo.sayHello()).to.equal("hello");
});
});
package.json
{
"name": "test_typescript",
"version": "1.0.0",
"scripts": {
"test": "mocha -r ts-node/register src/**/*.spec.ts"
},
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/mocha": "^2.2.41",
"chai": "^4.1.0",
"mocha": "^3.4.2",
"mocha-typescript": "^1.1.7",
"ts-node": "^3.2.0",
"typescript": "^2.4.1"
}
}
如何让测试识别命名空间?