Xeoncross
Xeoncross

Reputation: 57284

How do you include a Typescript namespace in a test file?

I have a Foo.ts file that contains the namespace Foo { ... } and some exported functions. I want to test the functions in this namespace using a test file right next to this one called Foo.test.ts.

///<reference path="./Foo.ts" />
import './Foo';
console.log(typeof Foo)

However, when I try to run the test file using mocha, I get an error.

 ReferenceError: Foo is not defined

How do I include the typescript namespace so I can access Foo.bar() and other exported functions?

Upvotes: 2

Views: 623

Answers (1)

Luke Becker
Luke Becker

Reputation: 894

It looks like there is pretty sufficient documentation on namespaces here: Typescript Namespaces

Your Foo.ts should look like this:

export namespace Foo {
 export function Bar() {
      return 'Bar';
 }
}

And then in the other file:

import * as Foo from './Foo';
console.log(typeof Foo)

I think you could do this to:

import { Foo } from './Foo';
console.log(typeof Foo)

Upvotes: 3

Related Questions