mirror of
https://github.com/electron/electron.git
synced 2026-01-08 23:18:06 -05:00
fix: importing from electron/utility in ESM (#47998)
This commit is contained in:
@@ -256,6 +256,41 @@ describe('utilityProcess module', () => {
|
||||
await once(child, 'exit');
|
||||
expect(log).to.equal(pathToFileURL(fixtureFile) + '\n');
|
||||
});
|
||||
|
||||
it('import \'electron/lol\' should throw', async () => {
|
||||
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-lol.mjs'), [], {
|
||||
stdio: ['ignore', 'ignore', 'pipe']
|
||||
});
|
||||
let stderr = '';
|
||||
child.stderr!.on('data', (data) => { stderr += data.toString('utf8'); });
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(1);
|
||||
expect(stderr).to.match(/Error \[ERR_MODULE_NOT_FOUND\]/);
|
||||
});
|
||||
|
||||
it('import \'electron/main\' should not throw', async () => {
|
||||
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-main.mjs'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/renderer\' should not throw', async () => {
|
||||
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-renderer.mjs'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/common\' should not throw', async () => {
|
||||
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-common.mjs'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/utility\' should not throw', async () => {
|
||||
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-utility.mjs'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pid property', () => {
|
||||
|
||||
@@ -65,6 +65,32 @@ describe('esm', () => {
|
||||
expect(result.code).to.equal(0);
|
||||
expect(result.stdout).to.equal('Exit with app, ready: false');
|
||||
});
|
||||
|
||||
it('import \'electron/lol\' should throw', async () => {
|
||||
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-lol.mjs'));
|
||||
expect(result.code).to.equal(1);
|
||||
expect(result.stderr).to.match(/Error \[ERR_MODULE_NOT_FOUND\]/);
|
||||
});
|
||||
|
||||
it('import \'electron/main\' should not throw', async () => {
|
||||
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-main.mjs'));
|
||||
expect(result.code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/renderer\' should not throw', async () => {
|
||||
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-renderer.mjs'));
|
||||
expect(result.code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/common\' should not throw', async () => {
|
||||
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-common.mjs'));
|
||||
expect(result.code).to.equal(0);
|
||||
});
|
||||
|
||||
it('import \'electron/utility\' should not throw', async () => {
|
||||
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-utility.mjs'));
|
||||
expect(result.code).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderer process', () => {
|
||||
@@ -213,5 +239,43 @@ describe('esm', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('electron modules', () => {
|
||||
it('import \'electron/lol\' should throw', async () => {
|
||||
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/lol";', {
|
||||
sandbox: false
|
||||
});
|
||||
expect(error).to.not.equal(null);
|
||||
expect(error?.message).to.match(/Cannot find package 'electron'/);
|
||||
});
|
||||
|
||||
it('import \'electron/main\' should not throw', async () => {
|
||||
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/main";', {
|
||||
sandbox: false
|
||||
});
|
||||
expect(error).to.equal(null);
|
||||
});
|
||||
|
||||
it('import \'electron/renderer\' should not throw', async () => {
|
||||
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/renderer";', {
|
||||
sandbox: false
|
||||
});
|
||||
expect(error).to.equal(null);
|
||||
});
|
||||
|
||||
it('import \'electron/common\' should not throw', async () => {
|
||||
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/common";', {
|
||||
sandbox: false
|
||||
});
|
||||
expect(error).to.equal(null);
|
||||
});
|
||||
|
||||
it('import \'electron/utility\' should not throw', async () => {
|
||||
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/utility";', {
|
||||
sandbox: false
|
||||
});
|
||||
expect(error).to.equal(null);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
3
spec/fixtures/api/utility-process/electron-modules/import-common.mjs
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/import-common.mjs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { net } from 'electron/common';
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/import-lol.mjs
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/import-lol.mjs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { net } from 'electron/lol';
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/import-main.mjs
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/import-main.mjs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { net } from 'electron/main';
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/import-renderer.mjs
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/import-renderer.mjs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { net } from 'electron/renderer';
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/import-utility.mjs
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/import-utility.mjs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { net } from 'electron/utility';
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/require-common.js
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/require-common.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const { net } = require('electron/common');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/require-lol.js
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/require-lol.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const { net } = require('electron/lol');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/require-main.js
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/require-main.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const { net } = require('electron/main');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/require-renderer.js
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/require-renderer.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const { net } = require('electron/renderer');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
3
spec/fixtures/api/utility-process/electron-modules/require-utility.js
vendored
Normal file
3
spec/fixtures/api/utility-process/electron-modules/require-utility.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const { net } = require('electron/utility');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
8
spec/fixtures/esm/electron-modules/import-common.mjs
vendored
Normal file
8
spec/fixtures/esm/electron-modules/import-common.mjs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const { net } = await import('electron/common');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
8
spec/fixtures/esm/electron-modules/import-lol.mjs
vendored
Normal file
8
spec/fixtures/esm/electron-modules/import-lol.mjs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const { net } = await import('electron/lol');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
8
spec/fixtures/esm/electron-modules/import-main.mjs
vendored
Normal file
8
spec/fixtures/esm/electron-modules/import-main.mjs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const { net } = await import('electron/main');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
8
spec/fixtures/esm/electron-modules/import-renderer.mjs
vendored
Normal file
8
spec/fixtures/esm/electron-modules/import-renderer.mjs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const { net } = await import('electron/renderer');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
8
spec/fixtures/esm/electron-modules/import-utility.mjs
vendored
Normal file
8
spec/fixtures/esm/electron-modules/import-utility.mjs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const { net } = await import('electron/utility');
|
||||
|
||||
process.exit(net !== undefined ? 0 : 1);
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BrowserWindow } from 'electron/main';
|
||||
import { BrowserWindow, utilityProcess } from 'electron/main';
|
||||
|
||||
import { expect } from 'chai';
|
||||
|
||||
@@ -82,6 +82,8 @@ describe('modules support', () => {
|
||||
});
|
||||
|
||||
describe('require(\'electron/...\')', () => {
|
||||
const utilityProcessFixturesPath = path.resolve(__dirname, 'fixtures', 'api', 'utility-process', 'electron-modules');
|
||||
|
||||
it('require(\'electron/lol\') should throw in the main process', () => {
|
||||
expect(() => {
|
||||
require('electron/lol');
|
||||
@@ -94,6 +96,17 @@ describe('modules support', () => {
|
||||
await expect(w.webContents.executeJavaScript('{ require(\'electron/lol\'); null }')).to.eventually.be.rejected();
|
||||
});
|
||||
|
||||
it('require(\'electron/lol\') should throw in the utility process', async () => {
|
||||
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-lol.js'), [], {
|
||||
stdio: ['ignore', 'ignore', 'pipe']
|
||||
});
|
||||
let stderr = '';
|
||||
child.stderr!.on('data', (data) => { stderr += data.toString('utf8'); });
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(1);
|
||||
expect(stderr).to.match(/Cannot find module 'electron\/lol'/);
|
||||
});
|
||||
|
||||
it('require(\'electron\') should not throw in the main process', () => {
|
||||
expect(() => {
|
||||
require('electron');
|
||||
@@ -118,6 +131,12 @@ describe('modules support', () => {
|
||||
await expect(w.webContents.executeJavaScript('{ require(\'electron/main\'); null }')).to.be.fulfilled();
|
||||
});
|
||||
|
||||
it('require(\'electron/main\') should not throw in the utility process', async () => {
|
||||
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-main.js'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('require(\'electron/renderer\') should not throw in the main process', () => {
|
||||
expect(() => {
|
||||
require('electron/renderer');
|
||||
@@ -130,6 +149,12 @@ describe('modules support', () => {
|
||||
await expect(w.webContents.executeJavaScript('{ require(\'electron/renderer\'); null }')).to.be.fulfilled();
|
||||
});
|
||||
|
||||
it('require(\'electron/renderer\') should not throw in the utility process', async () => {
|
||||
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-renderer.js'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('require(\'electron/common\') should not throw in the main process', () => {
|
||||
expect(() => {
|
||||
require('electron/common');
|
||||
@@ -141,6 +166,30 @@ describe('modules support', () => {
|
||||
w.loadURL('about:blank');
|
||||
await expect(w.webContents.executeJavaScript('{ require(\'electron/common\'); null }')).to.be.fulfilled();
|
||||
});
|
||||
|
||||
it('require(\'electron/common\') should not throw in the utility process', async () => {
|
||||
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-common.js'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
|
||||
it('require(\'electron/utility\') should not throw in the main process', () => {
|
||||
expect(() => {
|
||||
require('electron/utility');
|
||||
}).to.not.throw();
|
||||
});
|
||||
|
||||
it('require(\'electron/utility\') 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/utility\'); null }')).to.be.fulfilled();
|
||||
});
|
||||
|
||||
it('require(\'electron/utility\') should not throw in the utility process', async () => {
|
||||
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-utility.js'));
|
||||
const [code] = await once(child, 'exit');
|
||||
expect(code).to.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('coffeescript', () => {
|
||||
|
||||
Reference in New Issue
Block a user