Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import "describe" and "it" from mocha in TypeScript?

By default, when importing mocha in TypeScript, it brings in describe and it (and some others) into the global namespace.

Is there a way to bring in specific imports like import {describe, it} from 'mocha'?

like image 524
Ace Avatar asked Oct 02 '16 11:10

Ace


People also ask

What is Mocha and how do I use it?

So lets break it down. Remember, Mocha is a testing frameworks. That means it’s used to organize and execute tests. When writing a test, there are two basic function calls you should be aware of: describe () and it (). Both are used in our above example. describe () is simply a way to group our tests in Mocha.

How do you write a test in Mocha?

Tests structured in Mocha usually follow this template: Notice two key functions: describe () and it (). The describe () function is used to group similar tests. It’s not required for Mocha to run tests, but grouping tests make our test code easier to maintain.

What is DESC describe() in Mocha?

describe () is simply a way to group our tests in Mocha. We can nest our tests in groups as deep as we deem necessary. describe () takes two arguments, the first is the name of the test group, and the second is a callback function. Recall our earlier example.

What is mochajs test framework?

Mochajs, or simply Mocha, is a feature-affluent JavaScript test framework that runs test cases on Node JS and in the browser, making testing simple and fun. By running serially, Mocha JavaScript testing warrants flexibility and precise reporting, while mapping uncaught exceptions to the correct test cases.


2 Answers

Install mocha and its types:

npm install mocha --save-dev npm install @types/mocha --save-dev 

Then, simply import mocha in your test files:

import 'mocha';  describe('my test', () => {   it('does something', () => {     // your test   }); }); 
like image 131
Eryk Warren Avatar answered Oct 02 '22 09:10

Eryk Warren


Since TypeScript 2.0, you can add mocha to the types configuration of your tsconfig.json and it will always be loaded:

{   "compilerOptions": {     "types": [       "mocha"     ]   } } 
like image 38
jgillich Avatar answered Oct 02 '22 08:10

jgillich