Skip to content

Instantly share code, notes, and snippets.

@mrodalgaard
Last active July 21, 2017 09:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrodalgaard/8961e8760e6c408350f9 to your computer and use it in GitHub Desktop.
Save mrodalgaard/8961e8760e6c408350f9 to your computer and use it in GitHub Desktop.
Atom custom test runner with Mocha and Coverage
fs = require 'fs'
path = require 'path'
Mocha = require 'mocha'
coffeeCoverage = require 'coffee-coverage'
{allowUnsafeEval, allowUnsafeNewFunction} = require 'loophole'
module.exports = (args) ->
# Fixes CSP eval restriction
allowUnsafeEval ->
# TODO: Just use first test path for now
testPath = args.testPaths[0]
mocha = new Mocha(
ui: 'bdd'
reporter: 'html'
)
coffeeCoverage.register(
instrumentor: 'istanbul'
basePath: path.join(testPath, '..')
# basePath: path.join(testPath)
exclude: ['/spec', '/node_modules', '/.git']
coverageVar: coffeeCoverage.findIstanbulVariable()
writeOnExit: path.join(testPath, '..', 'coverage/coverage-coffee.json')
initAll: true
)
applicationDelegate = args.buildDefaultApplicationDelegate()
# Force spec window to start in development mode (Fixed in Atom 1.3.0-beta6)
win = applicationDelegate.getCurrentWindow()
if win.getUrl().indexOf('devMode%22%3Atrue') is -1
console.log 'Spec window not in devMode, redirecting...'
return win.loadUrl win.getUrl().replace('devMode%22%3Afalse', 'devMode%22%3Atrue')
# Create element for mocha reporter
element = document.createElement 'div'
element.id = 'mocha'
document.body.appendChild element
# Build atom global
window.atom = args.buildAtomEnvironment({
applicationDelegate, window, document
configDirPath: process.env.ATOM_HOME
enablePersistence: false
})
for file in fs.readdirSync(testPath)
if file.substr(-7) is '.coffee'
mocha.addFile path.join(testPath, file)
# Run tests and return a promise
new Promise((resolve, reject) ->
mocha.run((failures) ->
resolve(failures)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment