Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README with current node integration info #1018

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 62 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,74 @@ Create a new application with the following options:

### Node Integration

The Electron helpers provided by Spectron require accessing the core Electron
APIs in the renderer processes of your application. So, either your Electron
application has `nodeIntegration` set to `true` or you'll need to expose a
`require` window global to Spectron so it can access the core Electron APIs.
In order to gain access to the `electron` and `webContents` members of the
`Application` object using the best practices for Electron security, you must
disable `contextIsolation`, enable `enableRemoteModule`, and preload your contextBridge
differently.

You can do this by adding a [`preload`][preload] script that does the following:
It is recommended that you use `NODE_ENV == test && test_command` to start your application.
Create your `BrowserWindow` like this:

```js
if (process.env.NODE_ENV === 'test') {
```
var devTools = false
var contextIsolation = true
var enableRemoteModule = false
var nodeIntegration = false
if(process.env.NODE_ENV === 'test') {
devTools = false
contextIsolation = false
enableRemoteModule = true
nodeIntegration = true
}
win = new BrowserWindow({
width: 1600,
height: 800,
webPreferences: {
preload: require('path').join(__dirname, './preload.js'),
contextIsolation: contextIsolation,
enableRemoteModule: enableRemoteModule,
nodeIntegration: nodeIntegration
}
})
```
Write your preload like this:
```
if (process.env.NODE_ENV === 'test') {testMode = true}
const api = {
doThing: () => ipcRenderer.send('do-a-thing'),
... safeApiMethods
}
if (!isTestMode) {
contextBridge.exposeInMainWorld('electron', api)
} else {
function deepFreeze<T extends Record<string, any>>(o: T): Readonly<T> {
Object.freeze(o)
Object.getOwnPropertyNames(o).forEach(prop => {
if (o.hasOwnProperty(prop)
&& o[prop] !== null
&& (typeof o[prop] === 'object' || typeof o[prop] === 'function')
&& !Object.isFrozen(o[prop])) {
deepFreeze(o[prop])
}
})
return o
}
deepFreeze(api)
window.electronRequire = require
window.electron = api
}
```
Note that if you are writing typescript, and using webpack to build your project,
you may need to change `window.electronRequire = require` to
`window.electronRequire = eval('require');` to properly preload electron to the window.

The Electron helpers provided by Spectron require accessing the core Electron
APIs in the renderer processes of your application. So, either your Electron
application has `nodeIntegration` set to `true` or you'll need to expose a
`require` window global to Spectron so it can access the core Electron APIs.

Then create the Spectron `Application` with the `requireName` option set to
`'electronRequire'` and then runs your tests via `NODE_ENV=test npm test`.
See [693]: https://github.com/electron-userland/spectron/issues/693 for a detailed discussion
Copy link

@lacymorrow lacymorrow Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change "693" to "#693"

on this topic.

**Note:** This is only required if your tests are accessing any Electron APIs.
You don't need to do this if you are only accessing the helpers on the `client`
Expand Down