Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript + Jasmine / Mocha, but without global types?

I want to use a testing framework such as Jasmine or Mocha. However, doing so means adding their @types libraries via npm, and unfortunately these pollute the global namespace.

This means that when writing application code, intelisense contains testing functions, granted this is the most first world problem ever but it is still annoying to me.

I was hoping I could import the libraries at the top of each *.spec.ts file, but the definition files do not appear to support modules.

I found another library called Tape which uses module exports, however it is fairly basic and does not have a browser UI for the tests (they appear in the browser console).

Has anyone got any suggestions?

like image 750
Marlon Avatar asked Nov 30 '17 17:11

Marlon


People also ask

What is the difference between mocha and Jasmine framework?

Jasmine was created around 2008. The documentation describes Jasmine as “batteries included,” meaning that it attempts to provide everything a developer needs in a test framework. Mocha is younger than Jasmine, created around 2011. Mocha is not a “complete” test framework, and doesn't attempt to be.

Can Mocha run TypeScript?

TS-Mocha has one only dependency - ts-node, which is used as a TypeScript runtime to execute tests that can import and run imported TypeScript source files as well. It is a thin wrapper that run node process with mocha and set up ts-node environment to handle .

Does Jasmine support TypeScript?

jasmine : it is a testing framework for javascript code. It does not require DOM. @types/jasmine : it contains the type definition for Jasmine to be used with TypeScript.


1 Answers

One possible solution may be to add "types": [] to the tsconfig.json that compiles your application. Some of my projects are structured like this:

  • src contains the application code and has a tsconfig.json for compiling the application,

  • test contains test code and has a tsconfig.json appropriate for testing.

If the two files need to share configuration, I have a top-level tsconfig-base.json that the two other configuration files extend through "extends": "../tsconfig-base".

When I have a project structured like the above, just adding "types": [] to my src/tsconfig.json is enough to hide the Mocha globals from the application code. Note that some projects will need to list at least some packages instead of having an empty array. Initially, I thought that I would have to list in "types" each and every package my application uses, but that's not the case because, as stated in the documentation:

Specify "types": [] to disable automatic inclusion of @types packages.

Keep in mind that automatic inclusion is only important if you’re using files with global declarations (as opposed to files declared as modules). If you use an import "foo" statement, for instance, TypeScript may still look through node_modules & node_modules/@types folders to find the foo package.

Emphasis added.

Important caveat: some project structures can make it impossible to fix the issue by just using "types". I have a project structured differently from what I describe above. I'd have to refactor the build process for that project to be able to hide Mocha from the application files just by adding an appropriate value for "types".

like image 82
Louis Avatar answered Oct 29 '22 08:10

Louis