Electron 如何使用Mocha测试需要节点API的自定义模块&引用;无法读取属性';要求';“未定义”的定义;

Electron 如何使用Mocha测试需要节点API的自定义模块&引用;无法读取属性';要求';“未定义”的定义;,electron,mocha.js,spectron,Electron,Mocha.js,Spectron,我正在开发一个电子应用程序。我正在和摩卡和斯派克伦进行测试。摩卡在线路上出错了 const filebrowser = require("../src/filebrowser.js") 具体来说,当我尝试要求节点的fs模块时,第2行的filebrowser模块出现故障: const {remote} = require('electron'); const fs = remote.require('fs'); 我想这与Electron中的主进程/渲染器进程范围有关,但我不知道如何使其与Moc

我正在开发一个电子应用程序。我正在和摩卡和斯派克伦进行测试。摩卡在线路上出错了

const filebrowser = require("../src/filebrowser.js")
具体来说,当我尝试要求节点的fs模块时,第2行的filebrowser模块出现故障:

const {remote} = require('electron');
const fs = remote.require('fs');
我想这与Electron中的主进程/渲染器进程范围有关,但我不知道如何使其与Mocha正常工作。当我的模块依赖于我通常通过electron的远程模块访问的节点api时,我如何正确地在Mocha测试文件中要求我的模块

test/test.js(这是github页面上的spectron示例代码)。我通过package.json脚本(npm测试)使用“mocha”命令运行它。请注意,我甚至还没有为我的filebrowser模块编写测试,它在require语句中失败

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
const path = require('path')
const filebrowser = require("../src/filebrowser.js")

describe('Application launch', function () {
  this.timeout(10000)

  beforeEach(function () {
    this.app = new Application({
      path: electronPath,

      // use the main.js file in package.json located 1 level above.
      args: [path.join(__dirname, '..')]
    })

    return this.app.start()
  })

  afterEach(function () {
    if (this.app && this.app.isRunning()) {
        return this.app.stop()
    }
  })

  it('shows an initial window', function () {
    return this.app.client.getWindowCount().then(function (count) {
        assert.equal(count, 1)
    })
  })
})
src/filebrowser.js

const {remote} = require('electron');
const fs = remote.require('fs');
const Path = require('path');

module.exports = {        
    //note that I would be calling fs functions in here, but I never get that far because the error happens on remote.require('fs')
    determineFiletype: function(currentDirectory, fileName){}
}


经过更多的研究,斯派克伦似乎无法做到这一点。Spectron在Webdriver进程中启动,而不是在电子应用程序的主进程中启动。这适用于端到端测试,但不适用于普通模块测试。幸运的是,该模块对于模块测试非常有用。它允许您指定从哪个进程运行测试,以及要包含在主进程中的任何模块。最棒的是,它在Chromium中运行,因此您可以像正常情况一样访问应用程序的所有API