Created
April 22, 2017 11:17
-
-
Save amireh/2fc8f14d503ac9d1298f98d024ed9511 to your computer and use it in GitHub Desktop.
Example of how to set up a testing environment for Electron backend (main) and front-end (renderer) modules with 1) source files being processed by Webpack 2) tests run by Mocha 3) tests launched by Karma.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
module.exports = function (config) { | |
config.set({ | |
plugins: [ | |
'karma-electron', | |
'karma-mocha', | |
'karma-spec-reporter', | |
'karma-sourcemap-loader', | |
'karma-webpack', | |
], | |
browsers: [ 'VisibleElectron' ], | |
browserConsoleLogOptions: { | |
level: 'debug', | |
terminal: true, | |
}, | |
browserDisconnectTolerance: 0, | |
browserNoActivityTimeout: 2000, | |
concurrency: 1, | |
customLaunchers: { | |
VisibleElectron: { | |
base: 'Electron', | |
flags: [ '--show' ], | |
devTools: true, | |
entry: path.resolve(__dirname, 'ui/index--test-core-entry.js'), | |
browserWindowOptions: { | |
webPreferences: { | |
webSecurity: false, | |
allowRunningInsecureContent: true, | |
} | |
} | |
} | |
}, | |
frameworks: [ | |
'mocha' | |
], | |
files: [ | |
{ | |
pattern: 'ui/index--test.js', | |
watched: false, | |
included: true, | |
served: true, | |
}, | |
{ | |
pattern: 'ui/index--test-entry.js', | |
watched: false, | |
included: true, | |
served: true, | |
}, | |
], | |
preprocessors: { | |
'ui/index--test.js': [ 'electron' ], | |
'ui/index--test-entry.js': [ 'webpack', 'sourcemap' ], | |
}, | |
reporters: [ 'spec' ], | |
webpack: require('./webpack/test.js'), | |
client: { | |
captureConsole: true, | |
clearContext: true, | |
useIframe: false, | |
runInParent: true, | |
loadScriptsViaRequire: true, | |
}, | |
webpackServer: { | |
noInfo: true | |
} | |
}); | |
}; | |
// @file: ui/index--test-core-entry.js | |
// Here we'll load our main-process files. In my case, I have only a single | |
// module that responds to messages on `electron.ipcMain`. | |
require(require('path').resolve(__dirname, '../main/messageHandlers')); | |
// @file: ui/index--test.js | |
// Before we get webpack into play, we must rewrite Electron's window.require | |
// to another variable so that it doesn't conflict with webpack's. This assumes | |
// our application source files (that get processed by webpack) are using this | |
// identifier to include Electron modules. | |
if (!window.hasOwnProperty('electronRequire')) { | |
window.electronRequire = require; | |
delete window.require; | |
delete window.exports; | |
delete window.module; | |
} | |
// Now, window.require can be clobbered by webpack just fine, | |
// our app sources will use electronRequire() to get to Electron's. | |
// @file: index--test-entry.js | |
// Here we actually load the tests through Webpack. | |
const tap = (x, fn) => fn(x); | |
const loadTestFile = require.context('./', true, /\.test\.js$/); | |
loadTestFile.keys().forEach(loadTestFile); | |
process.on('uncaughtException', function handleUncaughtException (err) { | |
console.warn('uncaughtException in karma process!') | |
throw err; | |
}); | |
window.addEventListener('unhandledrejection', function(data) { | |
console.warn('Throwing unhandled promise rejection!'); | |
throw data.reason; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment