Skip to content

Instantly share code, notes, and snippets.

@banacorn
Last active December 12, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/bbea9272968167e0173b to your computer and use it in GitHub Desktop.
Save banacorn/bbea9272968167e0173b to your computer and use it in GitHub Desktop.
Catching errors in test scripts with Mocha in Atom
# 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