Skip to content

Commit

Permalink
spec: add a regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jun 30, 2021
1 parent ca730d4 commit 2bd8d54
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/worker/init.ts
Expand Up @@ -40,6 +40,5 @@ if (self.location.protocol === 'file:') {
if (appPath) {
// Search for module under the app directory.
global.module.paths = Module._nodeModulePaths(appPath);
console.log(global.module.paths);
}
}
20 changes: 20 additions & 0 deletions spec-main/chromium-spec.ts
Expand Up @@ -682,6 +682,26 @@ describe('chromium features', () => {
});
});

describe('web workers', () => {
let appProcess: ChildProcess.ChildProcessWithoutNullStreams | undefined;

afterEach(() => {
if (appProcess && !appProcess.killed) {
appProcess.kill();
appProcess = undefined;
}
});

it('Worker with nodeIntegrationInWorker has access to self.module.paths', async () => {
const appPath = path.join(__dirname, 'fixtures', 'apps', 'self-module-paths');

appProcess = ChildProcess.spawn(process.execPath, [appPath]);

const [code] = await emittedOnce(appProcess, 'exit');
expect(code).to.equal(0);
});
});

describe('form submit', () => {
let server: http.Server;
let serverUrl: string;
Expand Down
7 changes: 7 additions & 0 deletions spec-main/fixtures/apps/self-module-paths/index.html
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<script src="./renderer.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions spec-main/fixtures/apps/self-module-paths/main.js
@@ -0,0 +1,34 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');

function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false
}
});

mainWindow.loadFile('index.html');
}

ipcMain.handle('module-paths', (e, success) => {
process.exit(success ? 0 : 1);
});

app.whenReady().then(() => {
createWindow();

app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});

app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
4 changes: 4 additions & 0 deletions spec-main/fixtures/apps/self-module-paths/package.json
@@ -0,0 +1,4 @@
{
"name": "electron-test-self-module-paths",
"main": "main.js"
}
12 changes: 12 additions & 0 deletions spec-main/fixtures/apps/self-module-paths/renderer.js
@@ -0,0 +1,12 @@
const { ipcRenderer } = require('electron');

const worker = new Worker('worker.js');

worker.onmessage = (event) => {
const workerPaths = event.data.sort().toString();
const rendererPaths = self.module.paths.sort().toString();
const validModulePaths = workerPaths === rendererPaths && workerPaths !== 0;

ipcRenderer.invoke('module-paths', validModulePaths);
worker.terminate();
};
1 change: 1 addition & 0 deletions spec-main/fixtures/apps/self-module-paths/worker.js
@@ -0,0 +1 @@
self.postMessage(self.module.paths);

0 comments on commit 2bd8d54

Please sign in to comment.