From 7493062555b9eb4e6cc47d628a4831c121192f1f Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Tue, 11 Oct 2022 12:29:23 +0530 Subject: [PATCH] test: add tests for valid electron module names (#35931) * test: add tests for valid electron module names https://github.com/electron/electron/pull/35915 landed without any tests, so this change adds some. This also documents why these variations exist. Signed-off-by: Darshan Sen * fixup! doc: rephrase comment Signed-off-by: Darshan Sen * fixup! test: remove "Uncaught Error:" from error regex Signed-off-by: Darshan Sen Signed-off-by: Darshan Sen --- lib/common/reset-search-paths.ts | 7 ++++ spec/modules-spec.ts | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/lib/common/reset-search-paths.ts b/lib/common/reset-search-paths.ts index 1bbaf03fcfb..ebb72862a8c 100644 --- a/lib/common/reset-search-paths.ts +++ b/lib/common/reset-search-paths.ts @@ -52,6 +52,13 @@ if (process.type === 'renderer') { } const originalResolveFilename = Module._resolveFilename; + +// 'electron/main', 'electron/renderer' and 'electron/common' are module aliases +// of the 'electron' module for TypeScript purposes, i.e., the types for +// 'electron/main' consist of only main process modules, etc. It is intentional +// that these can be `require()`-ed from both the main process as well as the +// renderer process regardless of the names, they're superficial for TypeScript +// only. const electronModuleNames = new Set(['electron', 'electron/main', 'electron/renderer', 'electron/common']); Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean, options?: { paths: Array}) { if (electronModuleNames.has(request)) { diff --git a/spec/modules-spec.ts b/spec/modules-spec.ts index 3654100345b..77d32b84fb7 100644 --- a/spec/modules-spec.ts +++ b/spec/modules-spec.ts @@ -81,6 +81,68 @@ describe('modules support', () => { }); }); + describe('require(\'electron/...\')', () => { + it('require(\'electron/lol\') should throw in the main process', () => { + expect(() => { + require('electron/lol'); + }).to.throw(/Cannot find module 'electron\/lol'/); + }); + + it('require(\'electron/lol\') should throw in the renderer process', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + await expect(w.webContents.executeJavaScript('{ require(\'electron/lol\'); null }')).to.eventually.be.rejected(); + }); + + it('require(\'electron\') should not throw in the main process', () => { + expect(() => { + require('electron'); + }).to.not.throw(); + }); + + it('require(\'electron\') should not throw in the renderer process', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + await expect(w.webContents.executeJavaScript('{ require(\'electron\'); null }')).to.be.fulfilled(); + }); + + it('require(\'electron/main\') should not throw in the main process', () => { + expect(() => { + require('electron/main'); + }).to.not.throw(); + }); + + it('require(\'electron/main\') should not throw in the renderer process', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + await expect(w.webContents.executeJavaScript('{ require(\'electron/main\'); null }')).to.be.fulfilled(); + }); + + it('require(\'electron/renderer\') should not throw in the main process', () => { + expect(() => { + require('electron/renderer'); + }).to.not.throw(); + }); + + it('require(\'electron/renderer\') should not throw in the renderer process', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + await expect(w.webContents.executeJavaScript('{ require(\'electron/renderer\'); null }')).to.be.fulfilled(); + }); + + it('require(\'electron/common\') should not throw in the main process', () => { + expect(() => { + require('electron/common'); + }).to.not.throw(); + }); + + it('require(\'electron/common\') should not throw in the renderer process', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + await expect(w.webContents.executeJavaScript('{ require(\'electron/common\'); null }')).to.be.fulfilled(); + }); + }); + describe('coffeescript', () => { it('can be registered and used to require .coffee files', () => { expect(() => {