codematix
codematix

Reputation: 1337

TypeScript default import failing

I am trying to setup a project and run tests with mocha with mjackson/expect library for assertion. My code to be tested is:

// inside src/lib/math.ts
export function sum(a: number, b: number): number {
  return a + b;
}

and my test is as follows:

// inside src/tests/math.tests.ts
/// <reference path="../../typings/main/ambient/mocha/mocha.d.ts" />
/// <reference path="../../typings/main/ambient/expect/expect.d.ts" />

import expect from 'expect';

import {sum} from '../lib/math';

describe('sum', () => {
  it('should add two numbers', () => {
    expect(sum(1, 2)).toEqual(3);
  });
});

I am able to compile the code with tsc using the following command:

find src -name *.ts | xargs tsc --declaration --sourceMap --module commonjs --target es5 --listFiles --outDir .

However when I run mocha from my project directory using the following command:

mocha tests

I see the following error in my tests:

TypeError: expect_1.default is not a function

When I open the compiled version of my math.tests.ts, I see the following line at the top of the transpiled code:

var expect_1 = require('expect');

This is fine and as expected. However, when I look inside the test where expect is called, I see the following line:

expect_1.default(math_1.sum(1, 2)).toEqual(3);

Now this line of code seems wrong. The expect library is bundled as an ES6 module and the expect function is a default export from the module.

However, TypeScript compiler has emitted code into my test where it attempts to access a default attribute on expect_1 which is an import from the expect library. The expect_1 reference itself is the default exported function I need in my tests and not expect_1.default which is invalid.

A point to note is, if I modified my math.tests.ts to import expect using the older require syntax. Everything works fine.

Please help me understand what I am missing.

P.S. I use TypeScript 1.8.2 with Node v4.3.1.

Upvotes: 7

Views: 2895

Answers (3)

Bertrand L
Bertrand L

Reputation: 11

I had the same issue and fixed it by changing the import statement from

import expect from 'expect';

to

import * as expect from 'expect';

i.e. get the full module instead of just the default export

Upvotes: 1

Maciej Bukowski
Maciej Bukowski

Reputation: 3348

It looks like the expect namespace type is wrong in the npm package (@types/expect) You can make small workaround to save type checking:

import * as _expect from 'expect';
const expect = _expect as any as typeof _expect.default;

Upvotes: 3

vilicvane
vilicvane

Reputation: 12473

It seems that your expect package version does not match its declaration. I just installed one and found it does have a default entry:

exports['default'] = expect;
module.exports = exports['default'];

Upvotes: 0

Related Questions