william007
william007

Reputation: 18525

Mocha Unit Test for TypeScript in VS2015

I am using empty node.js typescript project. When I add the Mocha Unit Test for type script, it shows me that it can't find some keywords ("describe", "it") as follows: enter image description here

How should I resolve it?

Upvotes: 4

Views: 3491

Answers (2)

FunkySamuel
FunkySamuel

Reputation: 21

As @Michael Braude said@Michael Braude said, import mocha's d.ts file, now we can simply do this;

  1. Install mocha and mocha type define by

    npm i -D mocha @types/mocha
    
  2. Add this line in your *.test.ts file

    import 'mocha'
    

Upvotes: 2

Michael Braude
Michael Braude

Reputation: 6973

You'll need to add a d.ts file for Mocha so that the TypeScript compiler understands the library you're consuming. There are several ways to get this file:

  1. Go to http://definitelytyped.org/ and browse the GitHub repo for Mocha. It looks like you can find the one you want here: https://github.com/borisyankov/DefinitelyTyped/blob/ffceea9dd124d277c4597c7bd12930666ec074c5/mocha/mocha-node.d.ts
  2. Use tsd from the command line (http://definitelytyped.org/tsd/). This is an npm package that installs d.ts files from DefinitelyTyped. After installing, browse to the root directory you want to add the file to and run tsd install mocha-node from the command line.
  3. Use Nuget to find the Mocha d.ts file. First, click here:enter image description here then search for mocha and install this package:enter image description here.

After you've grabbed the d.ts file, you need to add a /// <reference> tag to your unit test so that Visual Studio finds the d.ts file and can parse it. Something like this - /// <reference path="typings/mocha/mocha.d.ts"/>.

Hope that helps.

Upvotes: 4

Related Questions