为什么黄瓜加载支持代码?

Why is cucumber loading support code?

我不得不说这可能是我遇到过的最疯狂的问题,我就是无法理解。我有一个 class;它名为 dashboard.js,位于我的 test/features/support 文件夹中。内容如下:

'use strict';

class Dashboard {
  constructor(browser) {
    this.browser = browser;
  }

  viewGame(id) {
    return this.browser.get(`/dashboard/game/${id}`);
  }

  getTeamName(color) {
    return browser.findElement(By.model(`game.teams.${color}.name`)).getAttribute('value');
  }
}

module.exports = Dashboard;

我正在使用量角器 运行 我的黄瓜测试。我涉及的所有测试在另一台机器上运行得非常好,自从我将它们加载到这台机器上以来我没有改变它们。机器之间没有区别;两者都运行在完全相同的环境中使用完全相同的 IOJS 版本。

运行 我的测试给了我这个错误。

[launcher] Error: TypeError: Class constructors cannot be invoked without 'new'
  at Object.Dashboard (c:\loluk\test\features\support\dashboard.js:4:14)
  at c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:62:25
  at Array.forEach (native)
  at Object.wrapper (c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:59:15)
  at Object.initializer (c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:20:37)

从我的测试代码中删除 require 到此支持文件并不能解决问题;黄瓜仍然加载它。我没有配置告诉它加载任何支持文件。因此,很自然地,我将文件夹从 support 更改为 lib。这应该可以解决问题 - 对吗?毕竟,它不叫支持。

[launcher] Error: TypeError: Class constructors cannot be invoked without 'new'
  at Object.Dashboard (c:\loluk\test\features\lib\dashboard.js:4:14)
  at c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:63:25
  at Array.forEach (native)
  at Object.wrapper (c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:60:15)
  at Object.initializer (c:\loluk\node_modules\cucumber\lib\cucumber\cli\support_code_loader.js:21:41)
  at Object.Library (c:\loluk\node_modules\cucumber\lib\cucumber\support_cod

没有。事实上,无论我将这个文件或它的文件夹重命名为什么,它总是会自行加载,尽管我从未告诉 Cucumber 这样做。更奇怪的是 Cucumber 不仅加载它,而且出于某种原因试图调用它。这意味着无论出于何种原因,它都试图执行我的 class - 它没有在另一台机器上执行。

有谁知道我怎样才能说服这只红黄瓜乖乖听话?只有将文件移出 features 文件夹才能解决此问题,但我不明白为什么它正在加载代码

你需要明白两件事。第一个是这样的:

$ cucumber.js --help 摘录:

-r, --require LIBRARY|DIR     Require files before executing the features. If
                              this option is not specified, all *.js and
                              *.coffee files that are siblings or below the
                              features will be loaded automatically. Automatic
                              loading is disabled when this option is specified,
                              and all loading becomes explicit.

                              Files under directories named "support" are
                              always loaded first.

第二件事是:当 Cucumber 为您加载 一个文件时,它会检查该文件导出的内容是否是一个函数。如果是,它将自动执行。这就是允许在简单函数 (module.exports = function () { this.Given(...) }) 中声明步骤定义的原因。如果您的其中一个文件导出 class 构造函数,它也将由 Cucumber 执行(没有 new 关键字),从而导致潜在问题。

最简单的修复方法是将 class 导出为 exports 的 属性,如下所示:

module.exports.Dashboard = class ...

显然,您需要稍微更改一下 require 语句:

var Dashboard = require('./path/to/dashboard.js').Dashboard;

另一种解决问题的方法是在 CLI 上明确使用 --require 标志,如帮助中所述。