我正在尝试用Spectron为我们的Electron App编写测试,但是我遇到了设置问题。我将经典设置与chai配合使用。我有一个包含安装代码的文件:

const path = require("path");
const { Application } = require("spectron");

module.exports = {
  initialiseSpectron: () => {
    let electronPath = path.join(__dirname, "../../node_modules", ".bin", "electron");

    if (process.platform == "win32") {
      electronPath += ".cmd";
    }

    return new Application({
      path: electronPath,
      args: [path.join(__dirname, "../index.ts"), path.join(__dirname, "../../package.json")],
      env: {
        ELECTRON_ENABLE_LOGGING: true,
        ELECTRON_ENABLE_STACK_DUMPING: true,
        NODE_ENV: "development"
      },
      startTimeout: 10000,
      chromeDriverLogPath: "../chromedriverlog.txt"
    });
  },
  sleep: time => new Promise(resolve => setTimeout(resolve, time))
};

然后测试本身:
const chaiAsPromised = require("chai-as-promised");
const chai = require("chai");
chai.should();
chai.use(chaiAsPromised);

const testHelper = require("./initialise");
const app = testHelper.initialiseSpectron();

// Setup Promises and start Application
before(() => app.start());

// Tear down App after Tests are finished
after(() => {
  if (app && app.isRunning()) {
    return app.stop();
  }
});

describe("Login", () => {
  it("opens a window", function() {
    return app.client
      .waitUntilWindowLoaded()
      .getWindowCount()
      .should.eventually.equal(1);
  });

  it("tests the title", () =>
    app.client
      .waitUntilWindowLoaded()
      .getTitle()
      .should.eventually.equal("VIPFY"));
});

我的问题是我总是会收到此错误:
 1) "before all" hook in "{root}"

  0 passing (2s)
  1 failing

  1) "before all" hook in "{root}":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

因此,看来该应用程序无法启动。但这是不正确的。将打开“应用程序窗口”,但是测试似乎无法识别出该窗口。我已经尝试使用各种语法更改路径。但是没有任何效果。我想念什么?

最佳答案

您是否尝试过增加 Mocha 的超时时间?

有时我第一次失败,然后进行第二次尝试。

在此处查看Electron 6的工作示例:

https://github.com/florin05/electron-spectron-example

09-19 22:30