Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Last active September 6, 2017 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhunzaker/5fc6f3ea153319e3140275914773d5bc to your computer and use it in GitHub Desktop.
Save nhunzaker/5fc6f3ea153319e3140275914773d5bc to your computer and use it in GitHub Desktop.
describe('Jest Electron Runner', function() {
it('has access to the window', function() {
expect(window).toEqual(global)
})
it('has access to the document', function() {
expect(document).toEqual(global.document)
})
})
'use strict'
const FakeTimers = require('jest-util').FakeTimers
const installCommonGlobals = require('jest-util').installCommonGlobals
const ModuleMocker = require('jest-mock')
class ElectronEnvironment {
constructor(config) {
const global = this.global = window
installCommonGlobals(global, config.globals)
this.moduleMocker = new ModuleMocker(global)
this.fakeTimers = new FakeTimers(global, this.moduleMocker, config)
}
dispose() {
if (this.fakeTimers) {
this.fakeTimers.dispose()
}
this.fakeTimers = null
}
runScript(script) {
return script.runInThisContext()
}
}
module.exports = ElectronEnvironment
'use strict'
const { app, BrowserWindow } = require('electron')
const qs = require('querystring')
const args = process.argv.slice(2)
const debug = args.indexOf('--debug') >= 0
function run () {
let runner = new BrowserWindow({
title: "Jest",
show: debug,
contextIsolation: true
})
require('jest')
let options = {
moduleDirectories: ["<rootDir>/tests"],
env: './src/jest-environment-electron'
}
runner.loadURL(`file://${__dirname}/runner.html?` + qs.stringify(options))
if (debug) {
runner.toggleDevTools()
}
runner.on('close', function () {
runner = null
})
}
app.on('ready', run)
app.on('window-all-closed', function () {
app.quit()
})
{
"name": "jest-electron",
"version": "1.0.0",
"description": "Executing jest with Electron",
"main": "main.js",
"scripts": {
"test": "electron ."
},
"keywords": ["jest", "electron"],
"author": "Nate Hunzaker <nate.hunzaker@gmail.com",
"license": "MIT",
"dependencies": {
"jest": "^17.0.3",
"electron": "^1.4.1"
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Jest</title>
</head>
<body>
<h2>Executing tests...</h2>
<p>Please reference the console for output.</p>
<script>
var jest = require('jest')
var qs = require('querystring')
var options = qs.parse(window.location.search)
jest.runCLI(options, __dirname, (content) => {
window.close()
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment