Last active
December 12, 2015 18:44
-
-
Save banacorn/bbea9272968167e0173b to your computer and use it in GitHub Desktop.
Catching errors in test scripts with Mocha in Atom
This file contains hidden or 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
# courtesy of @jccguimaraes, https://gist.github.com/jccguimaraes/2e08be6f549448d9361c | |
path = require 'path' | |
Mocha = require 'mocha' | |
module.exports = (args) -> | |
promise = new Promise (resolve, reject) -> | |
# build a headless Atom | |
window.atom = args.buildAtomEnvironment | |
applicationDelegate: args.buildDefaultApplicationDelegate() | |
window: window | |
document: document | |
configDirPath: process.env.ATOM_HOME | |
enablePersistence: false | |
testPath = path.join args.testPaths[0], 'test' | |
# using Mocha programatically | |
mocha = new Mocha().reporter('landing'); | |
Mocha.utils | |
.lookupFiles(testPath, ['coffee'], true) | |
.forEach mocha.addFile.bind(mocha) | |
# run! | |
runner = mocha.run (failure) -> | |
resolve failure | |
# catch and report errors occured in test scripts! | |
promise.catch (error) -> | |
# `error` is an instance of `Error` | |
console.dir error | |
# I don't know if there's other way to tell whether the spec was | |
# invoked from the editor, or ran in console, but ::isMaximized() seems do just fine | |
if window.atom.isMaximized() | |
# spec probably invoked in editor | |
window.atom.close() # close the spec runner if you like | |
else | |
# spec probably runs in console | |
process.exit 1 # do something to quit the process, or it HANGS! | |
return promise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment