I'm trying to compile TypeScript to a single JS file. Since I need it to be portable (and not run in a browser) I need to use namespaces instead of system or amd modules.
Issue: Can't get tests to run. Running npm test results in:
src/foo.spec.ts (6,20): Cannot find namespace 'Test'.
Here's my short sample program:
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"
}
}
How can I get the test to recognize the namespace?