vue/cli-plugin-unit-jest找不到模块

byqmnocz  于 2022-11-25  发布在  Vue.js
关注(0)|答案(1)|浏览(869)

我正在使用vue/cli-plugin-unit-jest,并根据文档设置后,我不断得到这个错误:

Cannot find module '@antmedia/webrtc_adaptor' from 'src/components/player.vue'

Require stack:
  src/components/player.vue
  src/components/Dashboard.vue
  src/views/Operation.vue
  src/router/index.js
  src/stores/modules/account.module.js
  src/stores/index.js
  tests/unit/cameras.spec.js

  61 | import config from '../config/config'
  62 | import { mapActions, mapState } from 'vuex'
> 63 | import { WebRTCAdaptor } from '@antmedia/webrtc_adaptor'
     | ^
  64 |
  65 | export default {
  66 |   name: 'player',

  at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
  at Object.<anonymous> (src/components/player.vue:63:1)
  at Object.<anonymous> (src/components/Dashboard.vue:18:1)

当我删除第63行时,测试开始通过,所以很明显,jest解析路径存在一些问题,但我找不到解决方案。
这是jest.config.js文件:

module.exports = {
  verbose: true,
  preset: '@vue/cli-plugin-unit-jest',
  ResolverOptions: {
    paths: [
      '<rootDir>/src'
    ]
  }

}
ubof19bj

ubof19bj1#

出现此问题的原因是,默认情况下,jest不会转换/node_modules中的任何内容。
对我有效的是:
替换import { WebRTCAdaptor } from '@antmedia/webrtc_adaptor'
使用import { WebRTCAdaptor } from '../../node_modules/@antmedia/webrtc_adaptor/src/main/webapp/js/webrtc_adaptor.js'
package.json中添加:

"jest" : {
  "preset": "@vue/cli-plugin-unit-jest",
  "transformIgnorePatterns": [
    "/node_modules/(?!@antmedia).+\\.js$"
  ]
},

当然,用您想要导入的包替换@antmedia

相关问题