Javascript Nightwatch Babel 7安装程序:意外标识符

Javascript Nightwatch Babel 7安装程序:意外标识符,javascript,babeljs,nightwatch.js,Javascript,Babeljs,Nightwatch.js,我最近在React项目中尝试了Babel 7。升级过程虽然复杂,但实际上相当顺利。然而,我被困在巴别塔7的夜视设置上。 我能找到的所有文档都与Babel 6有关,它们有些不同。我仍然试图尽我所能,但我在某一点上陷入了困境,没有找到解决办法。 我在React 16.8.3、Nightwatch 1.0.18和Babel 7上,根据官方文档更新了所有依赖项 这是我的夜表配置: require('@babel/register')({ extends: './.babelrc', extens

我最近在React项目中尝试了Babel 7。升级过程虽然复杂,但实际上相当顺利。然而,我被困在巴别塔7的夜视设置上。 我能找到的所有文档都与Babel 6有关,它们有些不同。我仍然试图尽我所能,但我在某一点上陷入了困境,没有找到解决办法。 我在React 16.8.3、Nightwatch 1.0.18和Babel 7上,根据官方文档更新了所有依赖项

这是我的夜表配置:

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升级)

在阅读了这篇文章之后:我还更新了我的.babelrc,用于我的夜视测试套装,如下所示:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false,
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

使用此配置,如果我尝试运行任何夜间监视测试,我会得到以下结果:

  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配置,但似乎无法使其正常工作。

好的,在浏览之后,我找到了一个用于babel 7的babel配置,该配置修复了此问题:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": "commonjs",
        "targets": {
          "node": "current"
        }
      }
    ]
  ],
  "plugins": [
    "add-module-exports",
  ]
}