Browse Source

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 <[email protected]>

* fixup! doc: rephrase comment

Signed-off-by: Darshan Sen <[email protected]>

* fixup! test: remove "Uncaught Error:" from error regex

Signed-off-by: Darshan Sen <[email protected]>

Signed-off-by: Darshan Sen <[email protected]>
Darshan Sen 2 years ago
parent
commit
7493062555
2 changed files with 69 additions and 0 deletions
  1. 7 0
      lib/common/reset-search-paths.ts
  2. 62 0
      spec/modules-spec.ts

+ 7 - 0
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<string>}) {
   if (electronModuleNames.has(request)) {

+ 62 - 0
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(() => {