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'
?
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.
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.
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.
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.
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 }); });
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" ] } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With