electron-userland / spectron

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

unknown error: Cannot read property 'electron' of undefined #140

Closed wojtkowiak closed 7 years ago

wojtkowiak commented 7 years ago

I have a very weird situation trying to test when nodeIntegration is set to false.

Error: unknown error: Cannot read property 'electron' of undefined
      at execute(<Function>) - D:\projekty\meteor-desktop\node_modules\spectron\lib\api.js:64:26

here it fails because process does not have versions...

Api.prototype.getVersion = function () {
  return this.app.client.execute(function () {
    if (typeof process === 'object') return process.versions.electron
  }).then(getResponseValue)
}

Here is my preload:

process.once('loaded', () => {
    let devtron = null;

    try {
        devtron = require('devtron'); // eslint-disable-line global-require
        global.__devtron = { require, process }; // eslint-disable-line no-underscore-dangle
    } catch (e) {
        // If that fails, then probably this is production build and devtron is not available.
    }
    if (process.env.NODE_ENV === 'test') {
        global.electronRequire = require;
    }
});

When I add global.process = process it works again:

    if (process.env.NODE_ENV === 'test') {
        global.electronRequire = require;
        global.process = process;
    }

Grepping code I have found this https://github.com/electron/electron/blob/master/lib/renderer/init.js#L123 but I have no idea whether this can be related.

kevinsawicki commented 7 years ago

This looks to be because process must be null which has a typeof object so it is trying to access versions on a null object.

Fix up in https://github.com/electron/spectron/pull/150

wojtkowiak commented 7 years ago

Thanks for the fix 👍