3

I wrote my mocha test in Typescript, and now I'm compiling it in es6

tsc *.ts --target es6 -m commonjs --watch

I get NO error in the console,
But in the WebStorm I get underlined every 'should' chai-keyword with red (when I hover it I get a message: ts2339: property 'should' does not exist on type Bluebird).

for example I'm using 'chai' and I want to check is expected value true:

import * as chai    from "chai";

export class WrapedChai {
     public shouldBeTrue(valueToTest : any){
        let expectedValue : boolean = true;
        expectedValue.should.equal(valueToTest);
    }
// or usintg SHOULD with a Promise
     public belongsToGLOErrorPromise(valueToTest: any) {
        let expectedValue   : boolean = true;
        return Promise.resolve(expectedValue).should.eventually.equal(valueToTest);
    } 
}

and should is always underlined in red.

I tried this:

import { should } from 'chai';
should();

and this:

import chai = require('chai');
var should = chai.should();

but 'should' stil stays underlined as an error.

Zack Zilic
  • 832
  • 3
  • 12
  • 28
  • 1
    hmm, I never worked with 'should' in context of typescript, but 'expect' worked fine. Btw. your code sample with the test and your later added imports seem not to match (as you import 'assert' in your sample) – Rhayene Aug 07 '17 at 10:02
  • 'expect' does not work for me (in this case). Like I said 'should' is working, and has NO error in Console, but it just gets underlined as an Error in Webstorm that 'should' does not exist on (in this case) 'expectedValue' or on true, or whatever, WebStorm tells me 'should' does not exist. – Zack Zilic Aug 07 '17 at 10:12

3 Answers3

3

This can be solved with npm install @types/chai.

Kevin
  • 24,871
  • 19
  • 102
  • 158
0

OK, I solved the problem.

FIRST WAY TO SOLVE IT (use a hack):
The Problem was with the types in TypeScript. Instead of writing

return Promise.resolve(isInstance).should.eventually.equal(expectedValue);

I split the line in two parts and wrote like this:

let helpVariable : any = Promise.resolve(isInstance);
return helpVariable.should.eventually.equal(expectedValue);

this way I'm applaying SHOULD on variable that has type any.

From some reason 'should' is underlined as an error if it is applied on any other type (like: boolean, int, number, string...)

SECOND WAY TO SOLVE IT (use 'export' keyword instead of 'should'):
first import the keyword 'export' like this:

  import {expect} from 'chai';

then, instead of writing:

return Promise.resolve(isInstance).should.eventually.equal(expectedValue);

write:

return export(Promise.resolve(isInstance)).eventually.equal(expectedValue);
Zack Zilic
  • 832
  • 3
  • 12
  • 28
0

The current version of Chai no longer supports types different than 'any'.

So your suggestion to cast to 'any' is good.

Also the previous version before 4.2.0 still support all types, so using the previous version can solve it and use this with no issues:

let expectedValue : boolean = true;
expectedValue.should.equal(valueToTest);

https://github.com/chaijs/chai/issues/1100

st_stefanov
  • 1,147
  • 1
  • 10
  • 28