玩笑未解析es6:SyntaxError:意外的令牌导入 [英] Jest not parsing es6: SyntaxError: Unexpected token import

查看:229
本文介绍了玩笑未解析es6:SyntaxError:意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的webpack项目设置Jest.当我运行测试时,Jest抱怨说它无法读取es6代码. Babel似乎无法转换我的测试文件.

I am trying to setup Jest with my webpack project. When I run my tests, Jest complains it cannot read es6 code. Babel seems to not transform my test files.

我尝试了各种在Internet上找到的解决方案,但仍然感到困惑.也许其他人具有更多的Babel/Webpack知识可以查看我的配置并为我提供帮助.

I have tried various solutions I have found on the internet but I'm still stumped. Maybe somebody with more Babel/Webpack knowledge can look at my config and help me out.

相关的package.json脚本:

relevant package.json script:

{
    "test": "jest --no-cache --config config/jest.config.js"
}

相关的package.json deps:

relevant package.json deps:

"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/preset-env": "^7.3.1",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.0.0",
"babel-loader": "^8.0.5",
"babel-plugin-styled-components": "^1.10.0",
"jest": "^24.0.0",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"

config/webpack.config.js:

config/webpack.config.js:

entry: './src/index.js',
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? 'none' : 'inline-source-map',
bail: true,
devServer: {
  contentBase: 'build',
  compress: true,
  port: 3000,
},
output: {
  path: path.resolve(__dirname, 'build'),
  filename: '[name].[chunkhash].js',
},
module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          cacheDirectory: true,
          presets: [
            '@babel/preset-env',
            '@babel/preset-react',
            '@babel/preset-flow',
          ],
          plugins: [
            'babel-plugin-styled-components',
            '@babel/plugin-proposal-class-properties',
          ],
        },
      },
    },
  ],
},
plugins: [
  new HtmlWebpackPlugin({
    template: 'src/index.html',
  }),
  new webpack.DefinePlugin({
    'process.env': JSON.stringify(process.env),
  }),
],

config/jest.config.js

config/jest.config.js

module.exports = {
  verbose: true,
  rootDir: '../',
  setupFiles: ['./config/jest.setup.js'],
  transform: {
    '^.+\\.js?$': 'babel-jest',
  },

config/jest.setup.js

config/jest.setup.js

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

开玩笑的错误: ●测试套件无法运行

jest error: ● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

...

Details:

/<...projectpath>/config/jest.setup.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Enzyme from 'enzyme';
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

我想要一些能正常工作的测试跑步者!我正在猜测我的转换:babel-jest在我的jest.config.js中什么也没做...

I want some a working test runner! Im guessing my transform: babel-jest is doing nothing in my jest.config.js...

推荐答案

您需要做两件事:

  1. 创建Babel配置文件(babel.config.js):

这是必需的,因为babel-jest依赖于传统的Babel配置文件,而不是webpack.从版本7开始,Babel已将JS配置支持为babel.config.js.

This is necessary because babel-jest relies on a traditional Babel config file, not webpack. Since version 7 Babel has supported JS configs as babel.config.js.

使用JS Babel配置时(例如,与.babelrc相反),Jest还会在node_modules中编译模块.按照惯例,AFAIK必须位于项目的根目录中,并附带jest配置文件.

When using a JS Babel config (as opposed to a .babelrc, for example) Jest also compiles modules in node_modules. AFAIK by convention this must be in the root of your project, alongside the jest configuration file.

这是基于webpack.config.js文件中的Babel选项的配置:

Here is a config based on the Babel options in your webpack.config.js file:

// babel.config.js
module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-flow',
  ],
  plugins: [
    'babel-plugin-styled-components',
    '@babel/plugin-proposal-class-properties',
  ]
}

  • 安装babel-core网桥版本:

  • Install the babel-core bridge version:

    npm install babel-core@7.0.0-bridge.0 --save-dev
    

    来自 github.com/babel/babel-bridge :

    此存储库包含我们所谓的桥接"程序包,该程序包旨在简化使用"babel-core"作为Babel 6的对等依赖项的库的过渡.

    This repo holds what we're calling a "bridge" package that is meant to ease the transition for libraries that use "babel-core" as a peer dependency for Babel 6.

    Babel 7过渡到范围的问题是,如果软件包依赖Babel 6,则他们可能希望同时添加对Babel 7的支持.由于Babel 7将以@ babel/core而不是babel-core的形式发布,因此维护人员无法在不进行重大更改的情况下进行该过渡.例如

    The issue with Babel 7's transition to scopes is that if a package depends on Babel 6, they may want to add support for Babel 7 alongside. Because Babel 7 will be released as @babel/core instead of babel-core, maintainers have no way to do that transition without making a breaking change. e.g.

  • 这篇关于玩笑未解析es6:SyntaxError:意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆