簡體   English   中英

Jest 導入重新導出的命名導出

[英]Jest importing re-exported named exports

我正在嘗試測試導入重新導出的命名導出的模塊。 除了標題建議的情況外,基本import語句正常工作(默認和命名)。

一個 repro-repo: https : //github.com/bali182/babel-jest-export-everything-bug (我認為這是 Jest 的一個問題,但在打開一個問題之后,開發人員建議這是一個配置問題,所以我問這里)

在這里演示這個問題:

包.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"
  }
}

命名導出.js

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

再出口.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>)

任何建議我在這里做錯了什么?

解決方案

發生這種情況是由於轉換運行時插件,為了修復它,只需將您的.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"
      ]
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM