简体   繁体   English

Jest 似乎不理解“导入类型”

[英]Jest doesn't seem to understand "import type"

We recently updated our monorepository to:我们最近将 monorepository 更新为:

  • Nx 14.3.6 NX 14.3.6
  • Angular 14.0.3角 14.0.3
  • Jest 28.1.1开玩笑 28.1.1
  • TypeScript 4.7.4打字稿 4.7.4

After the upgrade the compilation succeeded, but at runtime we got lots of errors like " emitDecoratorMetadata causes runtime errors by referencing type-only imports with namespaces " ( https://github.com/microsoft/TypeScript/issues/42624 ).升级后编译成功,但在运行时我们遇到了很多错误,例如“ emitDecoratorMetadata 通过使用命名空间引用仅类型导入导致运行时错误”( https://github.com/microsoft/TypeScript/issues/42624 )。 This error was also reported by ESLint. ESLint 也报告了这个错误。

We solved this by (for all types and interfaces) replacing all " import " statements to " import type " statements.我们通过(对于所有类型和接口)将所有“ import ”语句替换为“ import type ”语句来解决这个问题。 This fixed the runtime errors and the application worked again.这修复了运行时错误,应用程序再次运行。 To fix the ESLint error we also had to install and use the "eslint-plugin-import" extension.为了修复 ESLint 错误,我们还必须安装和使用“eslint-plugin-import”扩展。

So far so good, but now our tests stopped working.到目前为止一切顺利,但现在我们的测试停止工作了。 It seems that Jest doesn't understand the "import type" statement.看来 Jest 不理解“导入类型”语句。 In every unit test of a class that uses "import type", the tests fail with this error:在使用“导入类型”的类的每个单元测试中,测试失败并出现以下错误:

ReferenceError: Zyz is not defined ReferenceError: Zyz 未定义

(where xyz is an imported type in the tested class, eg (其中 xyz 是测试类中的导入类型,例如

// some-component.ts
import type { Xyz } from '...';
...

If we remove the "type" from the "import type" statement the test works but then the runtime errors reoccur.如果我们从“import type”语句中删除“type”,则测试可以正常工作,但运行时错误会再次出现。

I've searched quite a bit already (mostly by trying to use/reconfigure babel, because I found this post: https://github.com/babel/babel/issues/10981 ) but for now I'm unable to solve this problem.我已经搜索了很多(主要是通过尝试使用/重新配置 babel,因为我发现了这篇文章: https ://github.com/babel/babel/issues/10981)但现在我无法解决这个问题问题。

Seems to be fixed by the workaround explained here: https://github.com/thymikee/jest-preset-angular/issues/1199#issuecomment-1168802943似乎已通过此处解释的解决方法修复: https ://github.com/thymikee/jest-preset-angular/issues/1199#issuecomment-1168802943

So in tsconfig.spec.json configure the "include" property to contain all typescript files:因此在 tsconfig.spec.json 中配置“include”属性以包含所有打字稿文件:

// tsconfig.spec.json
{
  ...
  "include": ["**/*.ts"]
}

Previously we had:以前我们有:

// tsconfig.spec.json
{
  ...
  "include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
}

Edit: In addition to the above configuration it seems that ts-jest also requires non-isolatedModules.编辑:除了上述配置,似乎ts-jest还需要非隔离模块。 We were using isolatedModules : true, but it caused the same problem with type-only imports.我们使用了isolatedModules : true,但它导致了与仅类型导入相同的问题。

If anyone knows why or has a better idea: please let me know.如果有人知道原因或有更好的主意:请告诉我。

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

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