Skip to content

Commit

Permalink
test(e2e): run e2e tests with custom electron userDir
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Jan 16, 2019
1 parent 0c68261 commit d951809
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
9 changes: 9 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// If we are running in test mode, set the userData to a temporary location.
// This ensure that the app starts with a clean environment.
/**
* This module executes inside of electron's main process. You can start
* electron renderer process from here and communicate with the other processes
Expand All @@ -15,6 +17,7 @@ import get from 'lodash.get'
import path from 'path'
import url from 'url'
import os from 'os'
import fs from 'fs'
import querystring from 'querystring'
import { mainLog } from './lib/utils/log'
import ZapMenuBuilder from './lib/zap/menuBuilder'
Expand All @@ -23,6 +26,12 @@ import ZapUpdater from './lib/zap/updater'
import themes from './themes'
import { getDbName } from './store/db'

if (process.env.USER_DIR) {
const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'zap-'))
mainLog.info('Using temporary directory %s for userData', folder)
app.setPath('userData', folder)
}

/**
* Handler for open-link events.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
"app/node_modules",
"node_modules"
],
"setupTestFrameworkScriptFile": "./test/unit/__helpers__/setup-tests.js",
"setupTestFrameworkScriptFile": "./test/setup.js",
"transform": {
"^.+\\.js$": "babel-jest"
}
Expand Down
36 changes: 13 additions & 23 deletions test/e2e/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import { Application } from 'spectron'
import electronPath from 'electron'
import path from 'path'
import { startApp, stopApp } from './helpers'

jest.setTimeout(25000)
jest.unmock('electron')

describe('main window', function spec() {
describe('e2e tests', function spec() {
beforeAll(async () => {
this.app = new Application({
path: electronPath,
args: [path.join(__dirname, '..', '..', 'app')],
waitTimeout: 10000,
startTimeout: 10000,
quitTimeout: 2000,
requireName: 'electronRequire' // Use requre that we reference in preload.js
})

await this.app.start()
await this.app.client.waitUntilWindowLoaded()
this.app = await startApp()
})

afterAll(async () => {
if (this.app && this.app.isRunning()) {
await this.app.stop()
}
await stopApp(this.app)
})

it('opens a window', async () => {
const { client } = this.app

const windowCount = await client.getWindowCount()
expect(windowCount).toBeGreaterThanOrEqual(1)
})

it('should open window', async () => {
it('sets the window title as "Zap"', async () => {
const { browserWindow } = this.app

const title = await browserWindow.getTitle()
expect(title).toBe('Zap')
})

it("should haven't any logs in console of main window", async () => {
it("doesn't have any logs in console of main window", async () => {
const { client } = this.app

const logs = await client.getRenderProcessLogs()
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os from 'os'
import fs from 'fs'
import { Application } from 'spectron'
import electronPath from 'electron'
import path from 'path'

jest.setTimeout(50000)
jest.unmock('electron')

// Before running tests, create a new temporary directory that we will use for the electron userDir.
const userDir = fs.mkdtempSync(path.join(os.tmpdir(), 'zap-'))

export const startApp = async () => {
const app = new Application({
path: electronPath,
args: [path.join(__dirname, '..', '..', 'app')],
waitTimeout: 10000,
startTimeout: 10000,
quitTimeout: 2000,
// Use requre that we reference in preload.js
requireName: 'electronRequire',
// Tell the app to use a custom Electron userDir.
env: {
USER_DIR: userDir
}
})

await app.start()

await app.client.waitUntilWindowLoaded()
return app
}

export const stopApp = async app => {
if (app && app.isRunning()) {
await app.stop()
}
}
2 changes: 2 additions & 0 deletions test/unit/__helpers__/setup-tests.js → test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

configure({ adapter: new Adapter() })

jest.setTimeout(25000)

0 comments on commit d951809

Please sign in to comment.