我最近在我的React项目中尝试了一下Babel 7。尽管升级过程很复杂,但实际上是相当顺利的。然而,我被BABEL7的Nightwatch设置卡住了。我能找到的所有文档都是关于BABEL6的,它们有点不同。我仍然试图走得尽可能远,但我被困在了某个点上,没有找到解决方案的途径。我在React 16.8.3,Nightwatch 1.0.18和Babel 7上,我按照官方文档更新了所有的依赖项。
这是我的Nightwatch配置:
require('@babel/register')({
extends: './.babelrc',
extensions: '.js',
});
const reportBucket = 'admin';
const reportFolder = `./reports/${reportBucket}`;
const screenshotFolder = `/nightwatch/screenshots/${reportBucket}`;
module.exports = {
src_folders: ['./specs'],
output_folder: reportFolder,
page_objects_path: './pages',
custom_commands_path: './commands',
globals_path: './globals.js',
test_settings: {
default: {
launch_url: process.env.TEST_URL.replace(':443', ''),
selenium_host: process.env.SELENIUM_HOST,
selenium_port: process.env.SELENIUM_PORT,
silent: true,
end_session_on_fail: false, // keep session open to get screenshot on teardown
use_xpath: true,
request_timeout_options: {
timeout: 300000,
},
screenshots: {
enabled: true,
path: screenshotFolder,
on_failure: true,
on_error: true,
},
desiredCapabilities: {
'browserstack.use_w3c': 'true',
'browserstack.user': process.env.BROWSERSTACK_USER,
'browserstack.key': process.env.BROWSERSTACK_ACCESS_KEY,
'browserstack.local': true,
'browserstack.localIdentifier': process.env.BROWSERSTACK_LOCAL_ID,
'browserstack.selenium_version': process.env.SELENIUM_VERSION,
project: 'TEST',
build: process.env.BUILD_TAG,
name: process.env.SELENIUM_BROWSERS,
resolution: '1920x1080',
javascriptEnabled: true,
acceptSslCerts: true,
browserName: 'chrome',
acceptInsecureCerts: true,
},
},
chrome: {
desiredCapabilities: {
os: 'windows',
os_version: '10',
browserName: 'chrome',
browser_version: '71.0',
'goog:chromeOptions': {
args: [
'--enable-automation',
'--disable-web-security',
'--disable-infobars',
],
},
},
},
firefox: {
desiredCapabilities: {
/*
react-select does not work when the browser is not
the app in focus in the OS.
This was failing all tests that included this component
so we exculsively run on windows on browserstack
to avoid this issue
the core of the issue is that when firefox not in focus, 'mousedown'
is not triggered. All other selenium events seem fine (click, key, etc)
*/
os: 'windows',
os_version: '10',
browserName: 'firefox',
browser_version: '64.0',
marionette: true,
acceptInsecureCerts: true,
},
},
ie11: {
desiredCapabilities: {
/*
windows 10 needed so input values are
cleared and set properly. win 8 driver has issues
*/
os: 'windows',
os_version: '10',
browserName: 'ie',
browser_version: '11.0',
acceptInsecureCerts: true,
},
},
edge: {
desiredCapabilities: {
os: 'windows',
os_version: '10',
browserName: 'edge',
browser_version: '17.0',
acceptInsecureCerts: true,
},
},
},
parallel_process_delay: 1000,
live_output: true,
test_workers: {
enabled: true,
workers: parseInt(process.env.TEST_WORKERS, 10) || 1,
},
};请注意,我在顶部使用了@babel/register (从babel-register升级
在关注了这篇文章之后:https://babeljs.io/docs/en/v7-migration,我还更新了我的夜手表测试服的.babelrc,如下所示:
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"node": "current"
}
}
]
]
}使用这种配置,如果我尝试运行任何Nightwatch测试,我会得到以下结果:
TEST FAILURE: 1 error during execution 0 tests failed, 0 passed. 104ms
Unexpected identifier
import test from '../../../lib/random';
^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)有没有人能够解决类似的问题?我尝试了不同的babel配置,但似乎不能使其工作。
发布于 2019-03-01 05:53:21
好的,在四处寻找后,我发现了一个用于babel 7的babel配置,它解决了这个问题:
{
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
]
],
"plugins": [
"add-module-exports",
]
}https://stackoverflow.com/questions/54930706
复制相似问题