【问题标题】:jest - Unexpected token 'export'开玩笑 - 意外的令牌“出口”
【发布时间】:2021-10-28 21:22:45
【问题描述】:

我正在使用 vue.js 并尝试使用 jest(with vue utils test) async/await

  it('async/await test', async () => {
    await wrapper.setData({
      foo: 'bar',
    });
    // ...
  });

我可以使用 import 语句,但是当我运行 'yarn test'(jest) 命令时会出现此错误 但我没有从测试文件中导出任何内容

Details:
export default function _asyncToGenerator(fn) {
    ^^^^^^

    SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1517:14)
      at Object.<anonymous> (__test__/TestComponent.test.js:9:49)

这是我的配置文件

// jest.config.json

{
  "verbose": true,
  "testEnvironment": "node",
  "moduleFileExtensions": ["js", "vue"],
  "moduleNameMapper": {
    "^@/(.*)$": "<rootDir>/resources/assets/js/components/$1"
  },
  "transform": {
    "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
    ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
  },
  "snapshotSerializers": ["<rootDir>/node_modules/jest-serializer-vue"]
}

// babel.config.jss

module.exports = {
  presets: [
    '@vue/app',
    ['@babel/preset-env', { targets: { node: 'current' } }],
  ],
  plugins: [
    '@babel/plugin-proposal-nullish-coalescing-operator',
    '@babel/plugin-proposal-optional-chaining',
    '@babel/plugin-proposal-logical-assignment-operators',
    '@babel/plugin-proposal-numeric-separator',
  ],
};

环境

  System:
    OS: Windows 10 10.0.19043
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  Binaries:
    Node: 14.15.4 - C:\Program Files\nodejs\node.EXE     
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD     
    npm: 6.14.10 - C:\Program Files\nodejs\npm.CMD       
  npmPackages:
    jest: ^27.0.6 => 27.1.0
  +
  "@vue/test-utils": "^1.2.2"
  "@babel/core": "^7.15.0"
  "vue": "^2.6.10"

我该如何解决这个问题? 这是我的示例测试文件代码 https://gist.github.com/charmjeh/544455d49ff184750f17df8bb460b876

【问题讨论】:

标签: javascript vue.js jestjs


【解决方案1】:

我通过安装单元测试解决了这个问题并使用yarn test:unit

$ vue add unit-jest

【讨论】:

    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2017-07-04
    • 2018-04-04
    • 2018-12-07
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多