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 <raisinten@gmail.com>

* fixup! doc: rephrase comment

Signed-off-by: Darshan Sen <raisinten@gmail.com>

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

Signed-off-by: Darshan Sen <raisinten@gmail.com>

Signed-off-by: Darshan Sen <raisinten@gmail.com>
This commit is contained in:
Darshan Sen 2022-10-11 12:29:23 +05:30 committed by GitHub
parent e02de74ff2
commit 7493062555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View file

@ -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)) {

View file

@ -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(() => {