electron-userland / spectron

DEPRECATED: 🔎 Test Electron apps using ChromeDriver
http://electronjs.org/spectron
MIT License
1.68k stars 229 forks source link

Spectron 12 RequestError: read ECONNRESET after app.stop() #751

Open ChrisHSandN opened 3 years ago

ChrisHSandN commented 3 years ago

OS: Windows 10 Electron: 10.1.5 Spectron: 12.0.0

Electron 10 + Spectron 12 tests will pass (and successfully have access to the app via the webdriver.io functions) but will throw an uncaught ECONNRESET exception when stopping the app.


Minimal Reproducible Example:

package.json

{
  "main": "main.js",
  "dependencies": {
    "electron": "^10.1.5",
    "spectron": "^12.0.0",
    "mocha": "^8.2.0"
  }
}

main.js

const {app, BrowserWindow} = require('electron');

app.on('ready', () => {
    const mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: false
        }
    });

    // and load the index.html of the app.
    mainWindow.loadFile('about:blank');
});

app.on('window-all-closed', function () {
    app.quit();
});

spec.js

const Application = require('spectron').Application
const electronPath = require('electron')

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

    beforeEach(function () {
        this.app = new Application({
            path: electronPath,
            args: ['.']
        })
        return this.app.start()
    })

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

    it('shows an initial window', async function () {
        console.log(await this.app.client.getTitle());
    })
})

To Run

npm install
mocha spec.js

Error

xxx\spectron\node_modules\mocha\lib\runner.js:936
    throw err;
    ^

RequestError: read ECONNRESET
    at ClientRequest.<anonymous> (xxx\spectron\node_modules\webdriver\node_modules\got\dist\source\core\index.js:953:111)
    at Object.onceWrapper (events.js:418:26)
    at ClientRequest.emit (events.js:323:22)
    at ClientRequest.EventEmitter.emit (domain.js:482:12)
    at ClientRequest.origin.emit (xxx\spectron\node_modules\webdriver\node_modules\@szmarczak\http-timer\dist\source\index.js:39:20)
    at Socket.socketErrorListener (_http_client.js:426:9)
    at Socket.emit (events.js:311:20)
    at Socket.EventEmitter.emit (domain.js:482:12)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
    at TCP.onStreamRead (internal/stream_base_commons.js:205:27) {
  name: 'RequestError',
  code: 'ECONNRESET',
  timings: {
    start: 1604057887880,
    socket: 1604057887880,
    lookup: 1604057887880,
    connect: 1604057887880,
    secureConnect: undefined,
    upload: 1604057887880,
    response: undefined,
    end: undefined,
    error: 1604057888891,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 0,
      tcp: 0,
      tls: undefined,
      request: 0,
      firstByte: undefined,
      download: undefined,
      total: 1011
    }
  }
}
ChrisHSandN commented 3 years ago

I have found you can (very hackly) bypass the error being thrown by manually editing node_modules/webdriver/node_modules/got/dist/source/core/index.js

// add this condition ~line 956
else if (error.code === 'ECONNRESET') {
    return;
}
// before this exiting one
else {
    error = new RequestError(error.message, error, this);
}

It's no solution but it means you can at least get the Spectron tests running again!

baparham commented 2 years ago

are there any other ideas about this? Spectron 15/Electron 13 also seems to behave this way, except that this ECONNRESET error shows up when attempting to start another app instance after stopping the first. Now, of course, the version of got is newer, and the failure appears to originate from webdriver/build/utils.js