简体   繁体   English

打字稿+ Jasmine / Mocha,但没有全局类型?

[英]Typescript + Jasmine / Mocha, but without global types?

I want to use a testing framework such as Jasmine or Mocha. 我想使用一个测试框架,如Jasmine或Mocha。 However, doing so means adding their @types libraries via npm, and unfortunately these pollute the global namespace. 但是,这样做意味着通过npm添加他们的@types库,不幸的是,这会污染全局命名空间。

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. 这意味着在编写应用程序代码时,intelisense包含测试函数,这被认为是有史以来最先发生的问题,但它仍然让我讨厌。

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. 我希望我可以在每个*.spec.ts文件的顶部导入库,但定义文件似乎不支持模块。

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). 我找到了另一个名为Tape库,它使用模块导出,但是它非常基础,并且没有用于测试的浏览器UI(它们出现在浏览器控制台中)。

Has anyone got any suggestions? 有没有人有任何建议?

One possible solution may be to add "types": [] to the tsconfig.json that compiles your application. 一种可能的解决方案可能是将"types": []到编译应用程序的tsconfig.json中。 Some of my projects are structured like this: 我的一些项目结构如下:

  • src contains the application code and has a tsconfig.json for compiling the application, src包含应用程序代码,并有一个tsconfig.json用于编译应用程序,

  • test contains test code and has a tsconfig.json appropriate for testing. test包含测试代码,并有一个适合测试的tsconfig.json

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" . 如果这两个文件需要共享配置,我有一个顶级tsconfig-base.json ,其他两个配置文件通过"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. 当我有一个像上面这样结构的项目时,只需在我的src/tsconfig.json添加"types": []即可从应用程序代码中隐藏Mocha全局变量。 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 : 最初,我认为我必须在"types"列出我的应用程序使用的每个包,但事实并非如此,因为如文档中所述:

Specify "types": [] to disable automatic inclusion of @types packages. 指定"types": []禁用自动包含@types包。

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. 例如,如果使用import "foo"语句,TypeScript仍然可以查看node_modulesnode_modules/@types文件夹以查找foo包。

Emphasis added. 强调补充说。

Important caveat: some project structures can make it impossible to fix the issue by just using "types" . 重要警告:一些项目结构可能无法通过使用"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" . 我必须重构该项目的构建过程,以便能够通过为"types"添加适当的值来隐藏应用程序文件中的Mocha。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM