0

我有一个client包含 React 15.3、Webpack 4 和 Babel 7 的存储库。Webpack 很有魅力,但我们使用 Nightwatch 0.9.20 的 E2E 测试套件无法使用新@babel/register包进行编译。

我们公司正在将我们的 babel 版本从 6 升级到 7。

在互联网上流传一种将以下内容添加到babel.config.js文件中的解决方案:

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

在我们的例子中,这个解决方案并不能解决我们的问题。

我应该提到项目的结构如下:

client // where nightwatch is run from the terminal
  |-- tests // our E2E tests
  |-- nightwatch.conf.js
  |-- nighwatch_globals.js
  |-- babel.config.js

api // a completely separate repository
  |-- tests // we store factory definitions here as well
  |-- db
       |-- models // also a frequently referenced directory in our E2E tests

我们的 nightwatch.conf.js 结构如下所示:

require('@babel/register')(); // this has been upgraded from 'babel-register'
require('dotenv').config();

...

module.exports = {
  // nightwatch configurations

  "globals_path": "./nightwatch_globals.js",

  // more nightwatch configurations
}

我们的nightwatch_globals.js文件(调用错误的地方如下所示:

import fetch from 'isomorphic-fetch';

module.exports = {
  reporter: function(results, cb) {
    fetch('http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer').then(() => {
      cb();
      if (
        (typeof(results.failed) === 'undefined' || results.failed === 0) &&
          (typeof(results.error) === 'undefined' || results.error === 0)
      ) {
          process.exit(0);
      } else {
          process.exit(1);
      }
    });
  },
  // give the db some time to finish ops from previous test before
  // clearing db and starting the next test
  before: function(done) {
    require('./../eka-api/test/factories/index.js');
    done();
  },
  beforeEach: function(done) {
    setTimeout(function() {
      done();
    }, 5000);
  },
};

我们的babel.config.js文件如下:


module.exports = function(api) {
  api.cache(true);

  const presets = [
    "@babel/preset-env",
    "@babel/react",
    "@babel/preset-flow",
  ];

  const plugins = [
    "@babel/plugin-syntax-flow",
    "@babel/transform-flow-strip-types",
    ["@babel/plugin-proposal-decorators", {"legacy": true}],
    // react hot loader must come before class properties plugin
    "react-hot-loader/babel",
    // class properties plugin must come after decorators
    // if decorators has a 'legacy' attribute, class properties must be loose
    ["@babel/plugin-proposal-class-properties", {"loose" : true}],
    "@babel/plugin-transform-runtime"
  ];

  return {
    presets,
    plugins
  };
};

从我在终端中运行的客户端目录node nightwatch.conf.js ; nightwatch

这是持续的错误

/Users/myUser/Documents/api/test/factories/index.js:1
(function (exports, require, module, __filename, __dirname) { import bluebird from 'bluebird';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Module._compile (/Users/myUser/Documents/eka-client/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (module.js:580:10)
    at Object.newLoader [as .js] (/Users/myUser/Documents/eka-client/node_modules/pirates/lib/index.js:104:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)

我的直觉告诉我,无法识别导入令牌的原因与 Babel 7 如何将代码编译范围缩小到process.cwd(). 如您所见,错误来自 api。因为 nightwatch 是客户端目录中的一个包,所以 Babel 没有编译 api 目录(它仍然在 Babel 6 上)。但是,我想避免将整个客户端测试套件重构为独立于 api 文件。

我怀疑解决方案是找到一种@babel/register从客户端编译 api 的方法,但我不知道如何去做这样的事情。

4

0 回答 0