How to make es6 import/export works in Nightwatch test?

醉酒当歌 提交于 2020-12-05 03:39:17

问题


I want to import into my test file utility module, which is written in ES6 and exports several things I need. So what I do is

import { module } from 'file'

but it throws the 'SyntaxError: Unexpected token import' error.

I tried to:

  1. add require('babel-core/register') to the top of the nightwatch.conf.js
  2. add require('babel-register')() to the top of the nightwatch.conf.js
  3. npm i babel-plugin-add-module-exports --save-dev , and add "add-module-exports" plugin to the .babelrc config file
  4. npm i babel-preset-es2015 --save-dev , and add es2015 preset to the .babelrc config file
  5. add

    require('babel-core')
    require('babel-loader')
    require('babel-plugin-add-module-exports')
    require('babel-polyfill')
    require('babel-preset-stage-2')
    require('babel-preset-es2015')
    

to the top of the nightwatch.conf.js

All this didn't help. What should I do to make import/export works?


回答1:


Appears I was very close, the one thing missed from my attention:

I had

"presets": [
    ["es2015", { "modules": false }]
  ],

and { "modules": false } was the blocking thing. So, to make import/export works you need:

1 At the top of nightwatch.config

require('babel-register')() // or require('babel-core/register')

2 .babelrc should contain

{
  "presets": ["es2015"],
  "plugins": [
    "add-module-exports",
  ]
}

3 install babel

npm i babel-plugin-add-module-exports babel-core babel-preset-es2015 --save-dev


来源:https://stackoverflow.com/questions/42007248/how-to-make-es6-import-export-works-in-nightwatch-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!