【问题标题】:Jest importing re-exported named exports开玩笑导入再导出的命名导出
【发布时间】:2018-02-12 18:01:12
【问题描述】:

我正在尝试测试导入重新导出命名导出的模块。基本的import 语句正常工作(默认和命名),除了标题所暗示的情况。

repro-repo:https://github.com/bali182/babel-jest-export-everything-bug (我认为是 Jest 的问题,但是打开issue devs 后提示这是配置问题,所以我在这里问)

在这里演示问题:

package.json

{
  "name": "babel-jest-export-everything-bug",
  "scripts": {
    "test": "jest --config .jestrc.json"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^21.2.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "jest": "^21.2.1"
  }
}

.babelrc

{
  "presets": [
    [ "es2015", { "modules": "commonjs" } ],
    "stage-0",
    "react"
  ],
  "plugins": [
    "transform-decorators-legacy",
    "transform-runtime"
  ]
}

.jestrc.json

{
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  }
}

namedExports.js

export const x = 1
export const y = 2
export const z = 3

reExports.js

export * from './namedExports'
export default { hello: 'world' }

reExports.test.js

import Foo, { x, y, z } from './reExports'

describe('testing re-exports', () => {
  it('will never get to this method', () => {
    expect(x).toBe(1)
  })
})

然后失败

SyntaxError: Unexpected token import

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
  at Object.<anonymous> (src/reExports.test.js:1:120)
      at Generator.next (<anonymous>)

任何建议我在这里做错了什么?

【问题讨论】:

  • 使用babel-preset-env
  • 为什么需要一个'.jestrc.json'文件?
  • 我遇到了同样的问题 - 你卖掉了这个问题吗?
  • @DmitrySemenov 我放弃了使用这种导出/导入

标签: javascript jestjs


【解决方案1】:

解决方案

这是由于 transform-runtime 插件造成的,为了修复它,刚刚更新了您的 .babelrc 文件

["transform-runtime", { "polyfill": false }]

查看我的文件示例:

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": [
    "jsx-vue-functional",
    "transform-vue-jsx", ["transform-runtime", { "polyfill": false }],
    "transform-decorators-legacy",
    "lodash"
  ],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": [
        "transform-vue-jsx", 
        "transform-es2015-modules-commonjs", 
        "dynamic-import-node",
        "transform-decorators-legacy"
      ]
    },
    "cli": {
      "presets": ["env", "stage-2"],
      "plugins": [
        [ "babel-plugin-webpack-alias", { "config": "./aliases.config.js" } ],
        "transform-vue-jsx", 
        "transform-es2015-modules-commonjs", 
        "dynamic-import-node",
        "transform-decorators-legacy"
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 2017-05-20
    • 2019-03-10
    • 2019-11-04
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多