首页 > 解决方案 > ● 测试套件无法运行 Jest 遇到意外的令牌导出 { BsModalRef }

问题描述

使用 BS-MODAL 等 js 库时无法运行测试用例。细节:

/home/test-app/node_modules/ngx-bootstrap/modal/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { BsModalRef } from './bs-modal-ref.service';

标签: angularjestjs

解决方案


我将ngx-bootstrap添加到transformIgnorePatterns工作正常。

 "transformIgnorePatterns": [
      "node_modules/(?!ngx-bootstrap)"
    ]

如果在添加 ngx-bootstrap 后又出现一个错误

Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package)
  1. 安装

jest-canvas-mock

  1. 在里面添加配置jest config:setupFiles: ['<rootDir>/src/setupJest.ts', 'jest-canvas-mock']

最后在你的角度package.json文件中开玩笑链接这个

 "jest": {
 .........,
..........
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupJest.ts",
      "jest-canvas-mock"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!ngx-bootstrap)"
    ],
............,
............
  }

参考链接https://github.com/hustcc/jest-canvas-mock/issues/2#issuecomment-500307391

我遵循相同的步骤并为我工作。


推荐阅读