Add transform for stubbing electron

Summary:
The idea behind this is to enable tests running in pure node, without electron, because electron requires display drivers and complicated things.

I've been lazy and only added the bare minimum electron api's for the connection test to pass.
It would be awesome to auto generate it properly.

Reviewed By: danielbuechele

Differential Revision: D10238132

fbshipit-source-id: d4a2da2baf5f8964184b532a581c3a75ee33ef06
This commit is contained in:
John Knox
2018-10-22 08:58:54 -07:00
committed by Facebook Github Bot
parent c8a7ce5cfb
commit 87830ff106
3 changed files with 50 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
{
remote: {
process: {
env: {},
},
getCurrentWindow: function() {
return {
isFocused: function() {return true;}
};
},
},
}

View File

@@ -0,0 +1,33 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
var babylon = require('@babel/parser');
var fs = require('fs');
var electronStubs = babylon.parseExpression(
fs.readFileSync('static/electron-stubs.notjs').toString(),
);
module.exports = function(babel) {
return {
name: 'replace-electron-requires-with-stubs',
visitor: {
CallExpression(path) {
if (
path.node.type === 'CallExpression' &&
path.node.callee.type === 'Identifier' &&
path.node.callee.name === 'require' &&
path.node.arguments.length > 0
) {
if (path.node.arguments[0].value === 'electron') {
path.replaceWith(electronStubs);
}
}
},
},
};
};

View File

@@ -37,7 +37,11 @@ function transform({filename, options, src}) {
require('./dynamic-requires.js'), require('./dynamic-requires.js'),
]; ];
if (!options.isTestRunner) { if (options.isTestRunner) {
if (process.env.USE_ELECTRON_STUBS) {
plugins.push(require('./electron-stubs.js'));
}
} else {
// Replacing require statements with electronRequire to prevent metro from // Replacing require statements with electronRequire to prevent metro from
// resolving them. electronRequire are resolved during runtime by electron. // resolving them. electronRequire are resolved during runtime by electron.
// As the tests are not bundled by metro and run in @jest-runner/electron, // As the tests are not bundled by metro and run in @jest-runner/electron,