joh*_*hey 6 typescript jestjs angular type-only-import-export
我们最近将单一存储库更新为:
升级后编译成功,但在运行时我们遇到了很多错误,例如“ emitDecoratorMetadata通过引用带有命名空间的仅类型导入导致运行时错误”(https://github.com/microsoft/TypeScript/issues/42624)。ESLint 也报告了此错误。
我们通过(对于所有类型和接口)将所有“ import ”语句替换为“ import type ”语句来解决这个问题。这修复了运行时错误并且应用程序再次运行。为了修复 ESLint 错误,我们还必须安装并使用“eslint-plugin-import”扩展。
到目前为止一切顺利,但现在我们的测试停止工作了。看来 Jest 不理解“导入类型”声明。 在使用“导入类型”的类的每个单元测试中,测试都会失败并出现以下错误:
ReferenceError:Zyz 未定义
(其中 xyz 是测试类中的导入类型,例如
// some-component.ts
import type { Xyz } from '...';
...
Run Code Online (Sandbox Code Playgroud)
如果我们从“导入类型”语句中删除“类型”,则测试可以工作,但运行时错误会再次发生。
我已经搜索了很多(主要是尝试使用/重新配置 babel,因为我找到了这篇文章: https: //github.com/babel/babel/issues/10981),但现在我无法解决这个问题问题。
似乎已通过此处解释的解决方法修复: https://github.com/thymikee/jest-preset-angular/issues/1199#issuecomment-1168802943
因此,在 tsconfig.spec.json 中配置“include”属性以包含所有打字稿文件:
// tsconfig.spec.json
{
...
"include": ["**/*.ts"]
}
Run Code Online (Sandbox Code Playgroud)
之前我们有:
// tsconfig.spec.json
{
...
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
}
Run Code Online (Sandbox Code Playgroud)
编辑: 除了上述配置之外,ts-jest似乎还需要非isolatedModules。我们使用的是isolatedModules:true,但是它在仅类型导入中导致了同样的问题。
如果有人知道原因或有更好的想法:请告诉我。
| 归档时间: |
|
| 查看次数: |
2519 次 |
| 最近记录: |