发布于 4年前

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! vue-jest1@0.1.0 test:unit: `vue-cli-service test:unit`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the vue-jest1@0.1.0 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 即可。

©2020 edoou.com   京ICP备16001874号-3