jest 報錯 SyntaxError: Unexpected string

最近研究單元測試,使用 vue-cli3 新建的項目,然後一直報錯,其中錯誤如下

 ● Test suite failed to run

    /Users/lyc/Desktop/study/vue/vue-jest1/tests/unit/about.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import "core-js/modules/es6.array.find";
                                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    SyntaxError: Unexpected string

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.14s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test:unit: `vue-cli-service test:unit`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] test:unit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/lyc/.npm/_logs/2019-06-19T04_20_25_882Z-debug.log

找了很久最終在 https://github.com/vuejs/vue-... 找到解決方案。

需要使用 ./node_modules/jest/bin/jest.js --clearCach 或者 npm jest --clearCach,需要清除緩存?

然後再執行 npm run test:unit, 就可以了。

爲了避免每次都需要手動清除,直接加載 package.json文件裏,


  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:e2e": "vue-cli-service test:e2e",
    "test:unit": "jest --clearCache && vue-cli-service test:unit"
  },

這裏添加 jest --ClearCach 即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章