1. 程式人生 > 其它 >vue jest 報錯Cannot find module ‘babel-preset-local‘

vue jest 報錯Cannot find module ‘babel-preset-local‘

技術標籤:vue.js前端jest測試

vue專案後期新增jest測試問題
做測試的小夥伴一般有兩種情況亂:
1.新建vue專案 可以 一併安裝jest測試,
2.後期安裝jest
但是第二種就極其的不方便,依賴包的各種版本問題,是否支援ES5語法…等等…

在做第二種的時候我就遇到了這個棘手的問題:

在這裡插入圖片描述

 FAIL  tests/unit/helloworld.spec.js

    Cannot find module 'babel-preset-local'
    Require stack:
    - C:\Users\11466\Documents\triangle-admin\node_modules\
[email protected]
[email protected]@@babel\core\lib\config\files\plugins.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected][email protected]@@babel\core\lib\config\files\index.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected]
[email protected]@@babel\core\lib\index.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected]@babel-jest\build\index.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected][email protected]@@jest\transform\build\ScriptTransformer.js - C:\Users\11466\Documents\triangle-admin\node_modules\
[email protected]
[email protected]@@jest\transform\build\index.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected]@jest-runtime\build\index.js - C:\Users\11466\Documents\triangle-admin\node_modules\[email protected][email protected]@@jest\core\build\cli\index.js

3.這種情況提示我們缺少babel-preset-local依賴,
但是 用 npm install babel-preset-local 安裝依賴時又找不到該依賴包
那是因為這個包已經被淘汰了,我們可以使用新的依賴包:
新的依賴包名字就是:babel-preset-latest
用以下命令:

npm install --save-dev babel-preset-latest

安裝之後在.babelrc配置檔案中指定如下配置

{
        "presets": [
            ["latest",{
                "es2015": {
                    "modules": false
                }
            }]
        ]
    }

4.如果你用的是全域性配置babel.config.js 那就在babel.config.js檔案寫入:

module.exports = {
  presets: [
    // 使可以正常執行 vue 專案,
    '@vue/app',
    ["latest",{
      "es2015": {
          "modules": false
      }
  }]
  ],
}

5.現在可以跑測試了,萬事大吉

在這裡插入圖片描述