Angular 16迁移后Jest单元测试报错:模块外无法用import语句
Angular 16迁移后Jest单元测试报错问题
我们公司已将企业项目从Angular 12迁移至16,目前除Jest单元测试外,其余功能均正常运行。我推测问题出在TypeScript编译器或模块导入配置上,但无法定位具体原因。运行测试时抛出以下错误:
SyntaxError: Cannot use import statement outside a module 1 | import 'jest-preset-angular'; > 2 | import 'jest-preset-angular/setup-jest'; | ^ 3 | at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14) at Object.<anonymous> (node_modules/jest-preset-angular/setup-jest.js:2:24) at Object.<anonymous> (setup-jest.ts:2:1)
以下是项目相关配置文件内容:
package.json
{...}
tsconfig.json
{...}
tsconfig.spec.json
{...}
tsconfig.app.json
{...}
setup-jest.js
import 'jest-preset-angular'; import 'jest-preset-angular/setup-jest';
注:VS Code对上述两行代码给出如下提示:
Could not find a declaration file for module 'jest-preset-angular'... Could not find a declaration file for module 'jest-preset-angular/setup-jest'...
我尝试安装@types/jest-preset-angular但失败了,该包在npm仓库中不存在。
jest.config.js
{...}
angular.json
{...}
内容的提问来源于stack exchange,提问作者Emmanuel BRUNET
相关产品推荐
相关产品推荐

