Nightwatch Babel 7设置:意外的标识符

时间:2019-02-28 16:58:42

标签: javascript babeljs nightwatch.js

我最近在我的React项目中尝试了Babel 7。尽管很复杂,但升级路径实际上很顺利。但是,我仍然无法使用Babel 7进行Nightwatch的设置。 我能找到的所有文档都涉及Babel 6,它们有些不同。我仍然尽力去做,但是我被困在某个点,没有解决的办法。 我使用的是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配置,但似乎无法使其正常工作。

1 个答案:

答案 0 :(得分:0)

确定后,我发现babel 7的babel配置可以解决此问题:

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