How to import “describe” and “it” from mocha in TypeScript?

前端 未结 3 1512
说谎
说谎 2020-12-05 17:08

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

Is t

相关标签:
3条回答
  • 2020-12-05 17:41

    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
      });
    });
    
    0 讨论(0)
  • 2020-12-05 17:46

    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"
        ]
      }
    }
    
    0 讨论(0)
  • 2020-12-05 17:57

    I was having issues with errors and warnings, the problem stemmed from me renaming tsconfig.json to something else which makes Visual Studio Code enter "File Scope" instead of "Explicit Project". That made it impossible to import it without a red squiggly. Now that I've renamed the config back to tsconfig.json then import 'mocha'; works as Eryk mentioned.

    https://code.visualstudio.com/Docs/languages/typescript

    0 讨论(0)
提交回复
热议问题