如何在 Nightwatch 测试中使 es6 导入/导出工作? [英] How to make es6 import/export works in Nightwatch test?
问题描述
我想导入到我的测试文件实用程序模块中,该模块是用 ES6 编写的并导出我需要的一些东西.所以我要做的是
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.
我试图:
- 在 nightwatch.conf.js 顶部添加
require('babel-core/register')
- 在 nightwatch.conf.js 顶部添加
require('babel-register')()
npm i babel-plugin-add-module-exports --save-dev
,并将add-module-exports"插件添加到 .babelrc 配置文件中npm i babel-preset-es2015 --save-dev
,并将 es2015 预设添加到 .babelrc 配置文件中添加
- add
require('babel-core/register')
to the top of the nightwatch.conf.js - add
require('babel-register')()
to the top of the nightwatch.conf.js npm i babel-plugin-add-module-exports --save-dev
, and add "add-module-exports" plugin to the .babelrc config filenpm i babel-preset-es2015 --save-dev
, and add es2015 preset to the .babelrc config fileadd
require('babel-core')
require('babel-loader')
require('babel-plugin-add-module-exports')
require('babel-polyfill')
require('babel-preset-stage-2')
require('babel-preset-es2015')
到 nightwatch.conf.js 的顶部
to the top of the nightwatch.conf.js
所有这些都没有帮助.我应该怎么做才能使导入/导出正常工作?
All this didn't help. What should I do to make import/export works?
推荐答案
看起来我很亲近,但我忽略了一件事情:
Appears I was very close, the one thing missed from my attention:
我有
"presets": [
["es2015", { "modules": false }]
],
和 { "modules": false } 是阻塞的东西.因此,要使导入/导出工作,您需要:
and { "modules": false } was the blocking thing. So, to make import/export works you need:
1 在 nightwatch.config 的顶部
1 At the top of nightwatch.config
require('babel-register')() // or require('babel-core/register')
2 .babelrc 应该包含
2 .babelrc should contain
{
"presets": ["es2015"],
"plugins": [
"add-module-exports",
]
}
3 安装 babel
npm i babel-plugin-add-module-exports babel-core babel-preset-es2015 --save-dev
这篇关于如何在 Nightwatch 测试中使 es6 导入/导出工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!