Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support -w, --watch and --watch-extensions args #18

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Usage: electron-mocha [options] [files]
-s, --slow <ms> "slow" test threshold in milliseconds [75]
-t, --timeout <ms> set test-case timeout in milliseconds [2000]
-u, --ui <name> specify user-interface (bdd|tdd|exports)
-w, --watch watch files for changes
--check-leaks check for global variable leaks
--compilers use the given module(s) to compile files
--globals <names> allow the given comma-delimited global [names]
Expand All @@ -64,6 +65,7 @@ Usage: electron-mocha [options] [files]
--no-timeouts disables timeouts
--recursive include sub directories
--renderer run tests in renderer process
--watch-extensions additional extensions to monitor with --watch

```

Expand Down
2 changes: 2 additions & 0 deletions args.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function parse (argv) {
.option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]')
.option('-t, --timeout <ms>', 'set test-case timeout in milliseconds [2000]')
.option('-u, --ui <name>', 'specify user-interface (bdd|tdd|exports)', 'bdd')
.option('-w, --watch', 'watch files for changes')
.option('--check-leaks', 'check for global variable leaks')
.option('--compilers <ext>:<module>,...', 'use the given module(s) to compile files', list, [])
.option('--globals <names>', 'allow the given comma-delimited global [names]', list, [])
Expand All @@ -27,6 +28,7 @@ function parse (argv) {
.option('--no-timeouts', 'disables timeouts')
.option('--recursive', 'include sub directories')
.option('--renderer', 'run tests in renderer process')
.option('--watch-extensions <ext>,...', 'additional extensions to monitor with --watch')

program.on('globals', function (val) {
globals = globals.concat(list(val))
Expand Down
67 changes: 64 additions & 3 deletions mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function createFromArgs (args) {
extensions.push(ext)
})

if (args.watch) {
args.watchExtensions = args.watchExtensions ?
args.watchExtensions.split(',') : []
args.watchExtensions = args.watchExtensions.concat(extensions)
}

files = files.map(function (f) {
return path.resolve(f)
})
Expand All @@ -55,10 +61,65 @@ function createFromArgs (args) {
return mocha
}

function watch (mocha, args, callback) {
var utils = Mocha.utils
var runner
process.on('SIGINT', function(){
callback(0)
})

var watchFiles = utils.files(process.cwd(), [ 'js' ].concat(args.watchExtensions))
var runAgain = false

function loadAndRun () {
try {
runAgain = false
runner = mocha.run(function () {
runner = null
if (runAgain) {
rerun()
}
})
} catch(e) {
console.log(e.stack)
}
}

function purge () {
watchFiles.forEach(function (file) {
delete require.cache[file]
})
}

loadAndRun()

function rerun () {
purge()
if (!args.grep) mocha.grep(null)
mocha.suite = mocha.suite.clone()
mocha.suite.ctx = new Mocha.Context
mocha.ui(args.ui)
loadAndRun()
}

utils.watch(watchFiles, function () {
runAgain = true
if (runner) {
runner.abort()
} else {
rerun()
}
})
}

function run (args, callback) {
var mocha = createFromArgs(args)
/* var runner = */ mocha.run(callback)
// process.on('SIGINT', function () { runner.abort() })
var mocha = createFromArgs(args, callback)
if (args.watch) {
watch(mocha, args, callback)
} else {
/* var runner = */ mocha.run(callback)
// process.on('SIGINT', function () { runner.abort() })
}
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion renderer/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var mocha = require('../mocha')
}*/

// console.log(JSON.stringify(window.__args__, null, 2))
mocha.run(window.__args__, function (failureCount) {
var args = JSON.parse(JSON.stringify(window.__args__))
mocha.run(args, function (failureCount) {
ipc.send('mocha-done', failureCount)
})