Browse Source

test: fix a bunch of flaky tests related to emittedOnce (#15871)

Jeremy Apthorp 6 years ago
parent
commit
30109d64f2
2 changed files with 57 additions and 33 deletions
  1. 42 24
      spec/api-browser-window-spec.js
  2. 15 9
      spec/api-web-contents-spec.js

+ 42 - 24
spec/api-browser-window-spec.js

@@ -360,7 +360,6 @@ describe('BrowserWindow module', () => {
 
     it('is emitted after will-navigate on redirects', (done) => {
       let navigateCalled = false
-      w.loadURL(`${server.url}/navigate-302`)
       w.webContents.on('will-navigate', () => {
         navigateCalled = true
       })
@@ -368,6 +367,7 @@ describe('BrowserWindow module', () => {
         expect(navigateCalled).to.equal(true, 'should have called will-navigate first')
         done()
       })
+      w.loadURL(`${server.url}/navigate-302`)
     })
 
     it('is emitted before did-stop-loading on redirects', (done) => {
@@ -509,8 +509,9 @@ describe('BrowserWindow module', () => {
         height: 400,
         transparent: true
       })
+      const p = emittedOnce(w, 'ready-to-show')
       w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>')
-      await emittedOnce(w, 'ready-to-show')
+      await p
       w.show()
 
       const image = await w.capturePage()
@@ -1312,8 +1313,9 @@ describe('BrowserWindow module', () => {
             preload: preload
           }
         })
+        const p = emittedOnce(ipcMain, 'answer')
         w.loadFile(path.join(fixtures, 'api', 'preload.html'))
-        const [, test] = await emittedOnce(ipcMain, 'answer')
+        const [, test] = await p
         expect(test).to.be.an('object')
         expect(test.atPreload).to.be.an('array')
         expect(test.atLoad).to.be.an('array')
@@ -1428,8 +1430,9 @@ describe('BrowserWindow module', () => {
                 sandbox
               }
             })
+            const p = emittedOnce(ipcMain, 'remote')
             w.loadFile(path.join(fixtures, 'api', 'blank.html'))
-            const [, remote] = await emittedOnce(ipcMain, 'remote')
+            const [, remote] = await p
             expect(remote).to.equal('object')
           })
 
@@ -1443,8 +1446,9 @@ describe('BrowserWindow module', () => {
                 enableRemoteModule: false
               }
             })
+            const p = emittedOnce(ipcMain, 'remote')
             w.loadFile(path.join(fixtures, 'api', 'blank.html'))
-            const [, remote] = await emittedOnce(ipcMain, 'remote')
+            const [, remote] = await p
             expect(remote).to.equal('undefined')
           })
         })
@@ -1533,7 +1537,6 @@ describe('BrowserWindow module', () => {
         })
         const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
         const pageUrl = 'file://' + htmlPath
-        w.loadURL(pageUrl)
         ipcMain.once('answer', function (event, url) {
           let expectedUrl = pageUrl
           if (process.platform === 'win32') {
@@ -1542,6 +1545,7 @@ describe('BrowserWindow module', () => {
           assert.strictEqual(url, expectedUrl)
           done()
         })
+        w.loadURL(pageUrl)
       })
 
       it('should open windows in same domain with cross-scripting enabled', (done) => {
@@ -1556,7 +1560,6 @@ describe('BrowserWindow module', () => {
         ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
         const htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
         const pageUrl = 'file://' + htmlPath
-        w.loadURL(pageUrl)
         w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
           let expectedUrl = pageUrl
           if (process.platform === 'win32') {
@@ -1571,6 +1574,7 @@ describe('BrowserWindow module', () => {
             done()
           })
         })
+        w.loadURL(pageUrl)
       })
 
       it('should open windows in another domain with cross-scripting disabled', async () => {
@@ -1583,24 +1587,27 @@ describe('BrowserWindow module', () => {
         })
 
         ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
+        const browserWindowCreated = emittedOnce(app, 'browser-window-created')
+        const childLoaded = emittedOnce(ipcMain, 'child-loaded')
         w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'window-open-external' })
         const expectedPopupUrl = 'http://www.google.com/#q=electron' // Set in the "sandbox.html".
 
         // The page is going to open a popup that it won't be able to close.
         // We have to close it from here later.
         // XXX(alexeykuzmin): It will leak if the test fails too soon.
-        const [, popupWindow] = await emittedOnce(app, 'browser-window-created')
+        const [, popupWindow] = await browserWindowCreated
 
         // Wait for a message from the popup's preload script.
-        const [, openerIsNull, html, locationHref] = await emittedOnce(ipcMain, 'child-loaded')
+        const [, openerIsNull, html, locationHref] = await childLoaded
         expect(openerIsNull).to.be.true('window.opener is not null')
         expect(html).to.equal(`<h1>${expectedPopupUrl}</h1>`,
           'looks like a http: request has not been intercepted locally')
         expect(locationHref).to.equal(expectedPopupUrl)
 
         // Ask the page to access the popup.
+        const answer = emittedOnce(ipcMain, 'answer')
         w.webContents.send('touch-the-popup')
-        const [, exceptionMessage] = await emittedOnce(ipcMain, 'answer')
+        const [, exceptionMessage] = await answer
 
         // We don't need the popup anymore, and its parent page can't close it,
         // so let's close it from here before we run any checks.
@@ -1860,10 +1867,12 @@ describe('BrowserWindow module', () => {
             webviewTag: true
           }
         })
+        const didAttachWebview = emittedOnce(w.webContents, 'did-attach-webview')
+        const webviewDomReady = emittedOnce(ipcMain, 'webview-dom-ready')
         w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'))
 
-        const [, webContents] = await emittedOnce(w.webContents, 'did-attach-webview')
-        const [, id] = await emittedOnce(ipcMain, 'webview-dom-ready')
+        const [, webContents] = await didAttachWebview
+        const [, id] = await webviewDomReady
         expect(webContents.id).to.equal(id)
       })
     })
@@ -3197,13 +3206,13 @@ describe('BrowserWindow module', () => {
 
       showLastDevToolsPanel()
 
-      w.loadURL('about:blank')
-      w.webContents.openDevTools({ mode: 'bottom' })
-
       ipcMain.once('answer', function (event, message) {
         assert.strictEqual(message.runtimeId, 'foo')
         done()
       })
+
+      w.loadURL('about:blank')
+      w.webContents.openDevTools({ mode: 'bottom' })
     })
 
     it('serializes the registered extensions on quit', () => {
@@ -3417,32 +3426,39 @@ describe('BrowserWindow module', () => {
     })
 
     it('separates the page context from the Electron/preload context', async () => {
+      const p = emittedOnce(ipcMain, 'isolated-world')
       iw.loadFile(path.join(fixtures, 'api', 'isolated.html'))
-      const [, data] = await emittedOnce(ipcMain, 'isolated-world')
+      const [, data] = await p
       assert.deepStrictEqual(data, expectedContextData)
     })
     it('recreates the contexts on reload', async () => {
+      const loaded = emittedOnce(iw.webContents, 'did-finish-load')
       iw.loadFile(path.join(fixtures, 'api', 'isolated.html'))
-      await emittedOnce(iw.webContents, 'did-finish-load')
+      await loaded
+      const isolatedWorld = emittedOnce(ipcMain, 'isolated-world')
       iw.webContents.reload()
-      const [, data] = await emittedOnce(ipcMain, 'isolated-world')
+      const [, data] = await isolatedWorld
       assert.deepStrictEqual(data, expectedContextData)
     })
     it('enables context isolation on child windows', async () => {
+      const browserWindowCreated = emittedOnce(app, 'browser-window-created')
       iw.loadFile(path.join(fixtures, 'pages', 'window-open.html'))
-      const [, window] = await emittedOnce(app, 'browser-window-created')
+      const [, window] = await browserWindowCreated
       assert.ok(window.webContents.getLastWebPreferences().contextIsolation)
     })
     it('separates the page context from the Electron/preload context with sandbox on', async () => {
+      const p = emittedOnce(ipcMain, 'isolated-world')
       ws.loadFile(path.join(fixtures, 'api', 'isolated.html'))
-      const [, data] = await emittedOnce(ipcMain, 'isolated-world')
+      const [, data] = await p
       assert.deepStrictEqual(data, expectedContextData)
     })
     it('recreates the contexts on reload with sandbox on', async () => {
+      const loaded = emittedOnce(ws.webContents, 'did-finish-load')
       ws.loadFile(path.join(fixtures, 'api', 'isolated.html'))
-      await emittedOnce(ws.webContents, 'did-finish-load')
+      await loaded
+      const isolatedWorld = emittedOnce(ipcMain, 'isolated-world')
       ws.webContents.reload()
-      const [, data] = await emittedOnce(ipcMain, 'isolated-world')
+      const [, data] = await isolatedWorld
       assert.deepStrictEqual(data, expectedContextData)
     })
     it('supports fetch api', async () => {
@@ -3453,12 +3469,14 @@ describe('BrowserWindow module', () => {
           preload: path.join(fixtures, 'api', 'isolated-fetch-preload.js')
         }
       })
+      const p = emittedOnce(ipcMain, 'isolated-fetch-error')
       fetchWindow.loadURL('about:blank')
-      const [, error] = await emittedOnce(ipcMain, 'isolated-fetch-error')
+      const [, error] = await p
       fetchWindow.destroy()
       assert.strictEqual(error, 'Failed to fetch')
     })
     it('doesn\'t break ipc serialization', async () => {
+      const p = emittedOnce(ipcMain, 'isolated-world')
       iw.loadURL('about:blank')
       iw.webContents.executeJavaScript(`
         const opened = window.open()
@@ -3466,7 +3484,7 @@ describe('BrowserWindow module', () => {
         opened.close()
         window.postMessage({openedLocation}, '*')
       `)
-      const [, data] = await emittedOnce(ipcMain, 'isolated-world')
+      const [, data] = await p
       assert.strictEqual(data.pageContext.openedLocation, '')
     })
   })

+ 15 - 9
spec/api-web-contents-spec.js

@@ -137,12 +137,14 @@ describe('webContents module', () => {
       const oscillator = context.createOscillator()
       oscillator.connect(context.destination)
       oscillator.start()
+      let p = emittedOnce(webContents, '-audio-state-changed')
       await context.resume()
-      const [, audible] = await emittedOnce(webContents, '-audio-state-changed')
+      const [, audible] = await p
       assert(webContents.isCurrentlyAudible() === audible)
       expect(webContents.isCurrentlyAudible()).to.be.true()
+      p = emittedOnce(webContents, '-audio-state-changed')
       oscillator.stop()
-      await emittedOnce(webContents, '-audio-state-changed')
+      await p
       expect(webContents.isCurrentlyAudible()).to.be.false()
       oscillator.disconnect()
       context.close()
@@ -163,14 +165,16 @@ describe('webContents module', () => {
     it('can show window with activation', async () => {
       w.show()
       assert.strictEqual(w.isFocused(), true)
+      const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
       w.webContents.openDevTools({ mode: 'detach', activate: true })
-      await emittedOnce(w.webContents, 'devtools-opened')
+      await devtoolsOpened
       assert.strictEqual(w.isFocused(), false)
     })
 
     it('can show window without activation', async () => {
+      const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
       w.webContents.openDevTools({ mode: 'detach', activate: false })
-      await emittedOnce(w.webContents, 'devtools-opened')
+      await devtoolsOpened
       assert.strictEqual(w.isDevToolsOpened(), true)
     })
   })
@@ -496,7 +500,6 @@ describe('webContents module', () => {
     })
 
     it('can set the correct zoom level', (done) => {
-      w.loadURL('about:blank')
       w.webContents.on('did-finish-load', () => {
         w.webContents.getZoomLevel((zoomLevel) => {
           assert.strictEqual(zoomLevel, 0.0)
@@ -508,6 +511,7 @@ describe('webContents module', () => {
           })
         })
       })
+      w.loadURL('about:blank')
     })
 
     it('can persist zoom level across navigation', (done) => {
@@ -895,8 +899,9 @@ describe('webContents module', () => {
         }
       })
 
+      const p = emittedOnce(w.webContents, 'did-finish-load')
       w.loadURL('about:blank')
-      await emittedOnce(w.webContents, 'did-finish-load')
+      await p
 
       const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot')
 
@@ -926,8 +931,9 @@ describe('webContents module', () => {
         }
       })
 
+      const p = emittedOnce(w.webContents, 'did-finish-load')
       w.loadURL('about:blank')
-      await emittedOnce(w.webContents, 'did-finish-load')
+      await p
 
       const promise = w.webContents.takeHeapSnapshot('')
       return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
@@ -979,12 +985,12 @@ describe('webContents module', () => {
           sandbox: true
         }
       })
-      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
       w.webContents.once('did-finish-load', () => {
         const printers = w.webContents.getPrinters()
         assert.strictEqual(Array.isArray(printers), true)
         done()
       })
+      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
     })
   })
 
@@ -1006,7 +1012,6 @@ describe('webContents module', () => {
           sandbox: true
         }
       })
-      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
       w.webContents.once('did-finish-load', () => {
         w.webContents.printToPDF({}, function (error, data) {
           assert.strictEqual(error, null)
@@ -1015,6 +1020,7 @@ describe('webContents module', () => {
           done()
         })
       })
+      w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
     })
   })
 })