Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add the capacity to use node-inspector for child debugging #46

Closed
wants to merge 1 commit into from

4 participants

@migounette

Add the capacity to use node-inspector. It permits to debug the entire chain, from the calling program to the running test via exploring QUnit at the same time. In fact with this you can track and debug inside QUnit

@YannStephanHP YannStephanHP Add the capacity to use node-inspector on child process and to the vm…
… context created. It permits to debug the entire chain, from the calling program to the running test via exploring QUnit at the same time.
abe0cd6
@ifuller1

I'd be keen to see this included, helps with TDD if you're getting unexpected results.

@kof
Owner

Can this be a separate project?
I can't merge this for 2 reasons:
1. you didn't follow the code style
2. it needs explanation how to use for me and users

@kof
Owner

I can add something to make such stuff be possible from the outside.

@migounette

kof,

It was a proposal when node did'nt get the capability to invoke the debugger at runtime.
No, this pull-request is out dated. So, you can close it.

@kof kof closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 7, 2012
  1. @YannStephanHP

    Add the capacity to use node-inspector on child process and to the vm…

    YannStephanHP authored
    … context created. It permits to debug the entire chain, from the calling program to the running test via exploring QUnit at the same time.
This page is out of date. Refresh to see the latest.
Showing with 105 additions and 39 deletions.
  1. +92 −38 lib/child.js
  2. +13 −1 lib/testrunner.js
View
130 lib/child.js
@@ -1,6 +1,8 @@
var path = require('path'),
fs = require('fs'),
+ os = require('os'),
vm = require('vm'),
+ debug = require('vm'),
_ = require('underscore'),
trace = require('tracejs').trace;
@@ -18,33 +20,58 @@ sandbox = {
window: {setTimeout: setTimeout},
console: console,
clearTimeout: clearTimeout,
- Date: Date
+ Date: Date,
+ dbgTimeout: options.debug.timeout
};
-try {
- qunitCode = fs.readFileSync(qunitPath, 'utf-8');
-} catch( err ) {
- console.error('You are missing upstream QUnit library in deps/qunit');
- console.error('Using, maybe you forgot to update the submodules:');
- console.error('git submodule init && git submodule update');
- process.exit(1);
+function waitms(msToWait)
+{
+ var date = new Date();
+ var curDate = null;
+
+ do {
+ curDate = new Date();
+ debugger;
+ }
+ while(curDate-date < msToWait);
+}
+
+try
+{
+ if (process.platform === 'win32') {
+ console.log('Debug mode not yet supported under windows');
+ processScript();
+ }
+ else {
+ if (options.debug.vmbreak) {
+ options.debug.spawnbreak = true;
}
+ if (options.debug.spawnbreak) {
+ process.kill(process.pid, 'SIGUSR1');
+
+ // Now it's time to set a breakpoint
+ process.nextTick(function () {
+ waitms(options.debug.timeout);
+ processScript();
+ });
+ }
+ else {
+ processScript();
+ }
+ }
+}
+catch(err) {
+ console.log(err);
+}
-vm.runInNewContext(
- '(function(){'+ qunitCode +'}.call(window))',
- sandbox,
- qunitPath
-);
-
-// make qunit api global, like it is in the browser
-_.extend(global, sandbox.exports);
/**
* Require a resource.
* @param {Object} res
*/
function load(res) {
+
var requirePath = res.path.replace(/\.js$/, '');
// test resource can define'namespace'to expose its exports as a named object
@@ -63,6 +90,53 @@ function calcCoverage() {
}
/**
+ * Provide better stack traces
+ */
+var error = console.error;
+console.error = function(obj) {
+ // log full stacktrace
+ if (obj && obj.stack) {
+ obj = trace(obj);
+ }
+
+ return error.apply(this, arguments);
+};
+
+
+function processScript()
+{
+ try {
+ qunitCode = fs.readFileSync(qunitPath, 'utf-8');
+ } catch( err ) {
+ console.error('You are missing upstream QUnit library in deps/qunit');
+ console.error('Using, maybe you forgot to update the submodules:');
+ console.error('git submodule init && git submodule update');
+ process.exit(1);
+ }
+
+ code = (options.debug.vmbreak) ? 'function waitms(msToWait) { var date = new Date(); var curDate = null; do { curDate = new Date(); debugger; } while(curDate-date < msToWait); }; waitms(dbgTimeout);' : '';
+ code += '(function(){'+ qunitCode +'}.call(window))';
+
+ vm.runInNewContext(
+ code,
+ sandbox,
+ qunitPath
+ );
+
+ // make qunit api global, like it is in the browser
+ _.extend(global, sandbox.exports);
+
+ // require deps
+ options.deps.forEach(load);
+
+ // require code
+ load(options.code);
+
+ // require tests
+ options.tests.forEach(load);
+}
+
+/**
* Callback for each started test.
* @param {Object} test
*/
@@ -101,10 +175,12 @@ QUnit.testDone(function(data) {
});
});
+
/**
* Callback for all done tests in the file.
* @param {Object} res
*/
+
QUnit.done(_.debounce(function(data) {
if (options.coverage) {
data.coverage = calcCoverage();
@@ -115,25 +191,3 @@ QUnit.done(_.debounce(function(data) {
data: data
});
}, 1000));
-
-/**
- * Provide better stack traces
- */
-var error = console.error;
-console.error = function(obj) {
- // log full stacktrace
- if (obj && obj.stack) {
- obj = trace(obj);
- }
-
- return error.apply(this, arguments);
-};
-
-// require deps
-options.deps.forEach(load);
-
-// require code
-load(options.code);
-
-// require tests
-options.tests.forEach(load);
View
14 lib/testrunner.js
@@ -35,6 +35,18 @@ options = exports.options = {
// run test coverage tool
coverage: false,
+ // debug options
+ debug: {
+ // timeout if no debugger attached (in ms)
+ timeout: 5000,
+
+ // break at child spawn
+ spawnbreak: true,
+
+ // break in VM
+ vmbreak : true
+ },
+
// define dependencies, which are required then before code
deps: null,
@@ -150,7 +162,7 @@ exports.run = function(files, callback) {
}
if (opts.coverage) {
- converage.instrument(opts.code);
+ coverage.instrument(opts.code);
} else {
runOne(opts, finished);
}
Something went wrong with that request. Please try again.