7

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.

1
  • 1
    for me const expect = require('expect'); works Commented Sep 12, 2016 at 6:04

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much, that worked. Is anyone working on a pull request on this? Looks like the latest on github has already changed from what npm install can save.
1

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

Comments

0

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'];

3 Comments

Yes I did see that too, but it still doesn't get imported correctly. I tried the same library with babel. It seems to use the default entry too and works perfectly. So what could be a solution to this?
Babel handles default entry differently, as I remember it would fall back to the entire exported module if no default entry found. But your code should work with current version of expect, try debugging it under JavaScript level and find out what's wrong.
Nothing seemed to work. Not sure if its a Typescript issue or an expect library issue. I've switched over to chai.js.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.