Jest 无法处理使用参数导入的 SVG 文件

Jest can't process SVG files imported with an argument

在“vue-svg-loader”模式下 @nuxtjs/svg,SVG 导入方式如下: import ArrowRight from '~/assets/img/arrow-right.svg?inline'

但是 Jest 的“?inline”部分有问题:

Could not locate module ~/assets/img/arrow-right.svg?inline mapped as:
D:\path\to\project$1.

Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/^~\/(.*)$/": "D:\path\to\project$1"
  },
  "resolver": undefined
}

我尝试了给 previous similar question 的两个答案,但在每种情况下我都遇到了相同的错误。

关于如何解决这个问题有什么建议吗?

对于遇到此问题的任何人,解决方案是 here

jest.config.js

module.exports = {
  moduleFileExtensions: ["js", "jsx", "json", "vue"],
  transform: {
    "^.+\.svg": "<rootDir>/svgTransform.js",
    "^.+\.vue$": "vue-jest",
    ".+\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$": "jest-transform-stub",
    "^.+\.jsx?$": "babel-jest"
  },
  moduleNameMapper: {
    "^@/(.*svg)(\?inline)$": "<rootDir>/src/",
    "^@/(.*)$": "<rootDir>/src/"
  },
  snapshotSerializers: ["jest-serializer-vue"],
  testMatch: ["**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"],
  testURL: "http://localhost/",
  collectCoverageFrom: ["!**/src/__mocks__/**", "**/src/**/*.{js,vue}"]
};

关键点如下:

“您感兴趣的是这一行,"^@/(.*svg)(\?inline)$": "<rootDir>/src/"。注意将其添加到默认正则表达式上方,"^@/(.*)$": "<rootDir>/src/",此处以顺序为准。”