You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:09:51