From fdee0ef7b723e9949955ffb9d12dfc0fda59a645 Mon Sep 17 00:00:00 2001 From: John Knox Date: Fri, 18 Oct 2019 10:06:29 -0700 Subject: [PATCH] Use jest to mock electron in tests Summary: Replacing our custom electron-stubs transform with a mocked module. This will now be the default impl for tests, and we can override it by mocking as needed. Reviewed By: passy Differential Revision: D17979659 fbshipit-source-id: a26994158d439fa7dc1ca38225294106fc4e1559 --- __mocks__/electron.tsx | 23 +++++++++++++++++++++++ src/__tests__/server.device.tsx | 7 +++++++ static/transforms/index.js | 8 ++------ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 __mocks__/electron.tsx diff --git a/__mocks__/electron.tsx b/__mocks__/electron.tsx new file mode 100644 index 000000000..05628cac2 --- /dev/null +++ b/__mocks__/electron.tsx @@ -0,0 +1,23 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +module.exports = { + remote: { + process: { + env: {}, + }, + app: { + getAppPath: process.cwd, + getVersion: () => '0.9.99', + relaunch: () => {}, + exit: () => {}, + }, + getCurrentWindow: () => ({isFocused: () => true}), + }, +}; diff --git a/src/__tests__/server.device.tsx b/src/__tests__/server.device.tsx index fe45c9af7..5ae7d73a1 100644 --- a/src/__tests__/server.device.tsx +++ b/src/__tests__/server.device.tsx @@ -7,6 +7,13 @@ * @format */ +jest.mock('../fb-stubs/Logger'); +try { + jest.mock('../fb/Logger'); +} catch { + // Allowed to fail when fb modules are not present. +} + import Server from '../server'; import {init as initLogger} from '../fb-stubs/Logger'; import reducers, {Store} from '../reducers/index'; diff --git a/static/transforms/index.js b/static/transforms/index.js index 1e26f7268..cdcd9f8c9 100644 --- a/static/transforms/index.js +++ b/static/transforms/index.js @@ -63,12 +63,8 @@ function transform({filename, options, src}) { if (process.env.BUILD_HEADLESS) { plugins.push(require('./electron-stubs.js')); - plugins.push(require('./electron-requires.js')); - } else if (options.isTestRunner) { - if (process.env.USE_ELECTRON_STUBS) { - plugins.push(require('./electron-stubs.js')); - } - } else { + } + if (!options.isTestRunner) { // Replacing require statements with electronRequire to prevent metro from // resolving them. electronRequire are resolved during runtime by electron. // As the tests are not bundled by metro and run in @jest-runner/electron,