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

How to debug Mocha tests? #51

Closed
MarkHerhold opened this issue Aug 30, 2015 · 4 comments
Closed

How to debug Mocha tests? #51

MarkHerhold opened this issue Aug 30, 2015 · 4 comments
Labels

Comments

@MarkHerhold
Copy link

I cannot get iron-node to work with mocha. I have seen and read the page on "How to debug other Node.js command line applications like a Grunt task?" but that did not help.

I am able to debug things running in mocha just fine with node-inspector using:

node-debug ./node_modules/mocha/bin/_mocha ./spec/mytest.spec.js

I have tried the same thing with iron-node along with passing in different arguments.

This works, but the tests all run and don't trigger the debugger.

iron-node ./node_modules/mocha/bin/_mocha -- ./spec/mytest.spec.js

I am using the latest version of iron-node and mocha.
Environment: iojs 3.2.0, Ubuntu 15

@s-a
Copy link
Owner

s-a commented Aug 31, 2015

Have you tried the keyword debugger;? There is also a doc.https://github.com/s-a/iron-node/blob/master/docs/MASTER-THE-BREAKPOINTS.md which might be interesting.

@MarkHerhold
Copy link
Author

Sorry, yes I have tried the debugger; statement. I am not sure the source even gets loaded so I don't think I can set a breakpoint.

@s-a
Copy link
Owner

s-a commented Aug 31, 2015

Hmm, yes mocha spawns another process where it does not work anymore.
image

I am not sure why people decide to use such a strategy. However to make it work we need to create an alias and change the call to iron-node instead of node

alias for shell (on windows):

# locate 
$ where mocha;
# --> C:\Users\Stephan\AppData\Roaming\npm\mocha.cmd

alias file one:

$ cp c:\Users\Stephan\AppData\Roaming\npm\mocha.cmd c:\Users\Stephan\AppData\Roaming\npm\iron-mocha.cmd;

edit iron-mocha.cmd

:: Created by npm, please don't edit manually.
@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\mocha\bin\iron-mocha" %*
) ELSE (
  node  "%~dp0\node_modules\mocha\bin\iron-mocha" %*
)

alias file two:

$ cp c:\Users\Stephan\AppData\Roaming\npm\node_modules\mocha\bin\mocha c:\Users\Stephan\AppData\Roaming\npm\node_modules\mocha\bin\iron-mocha;

edit iron-mocha: and add process.argv[0] = "iron-node.cmd";

# c:\Users\Stephan\AppData\Roaming\npm\node_modules\mocha\bin\iron-mocha:
#!/usr/bin/env node

/**
 * This tiny wrapper file checks for known node flags and appends them
 * when found, before invoking the "real" _mocha(1) executable.
 */

var spawn = require('child_process').spawn
  , args = [ __dirname + '/_mocha' ];

process.argv.slice(2).forEach(function (arg) {
  switch (arg) {
    case '-d':
      args.unshift('--debug');
      break;
    case 'debug':
    case '--debug':
    case '--debug-brk':
      args.unshift(arg);
      break;
    case '-gc':
    case '--expose-gc':
      args.unshift('--expose-gc');
      break;
    case '--gc-global':
    case '--harmony':
    case '--harmony-proxies':
    case '--harmony-collections':
      args.unshift(arg);
      break;
    default:
      if (0 == arg.indexOf('--trace')) args.unshift(arg);
      else args.push(arg);
      break;
  }
});

process.argv[0] = "iron-node.cmd";
var proc = spawn(process.argv[0], args, { customFds: [0,1,2] });
proc.on('exit', function (code, signal) {
  process.on('exit', function(){
    if (signal) {
      process.kill(process.pid, signal);
    } else {
      process.exit(code);
    }
  });
});

done

$ cd your-projectfolder;
$ iron-mocha; # instead of mocha

image

@s-a s-a added the question label Aug 31, 2015
@s-a s-a changed the title Use with Mocha How to debug Mocha tests? Sep 1, 2015
@s-a s-a closed this as completed Sep 1, 2015
@s-a
Copy link
Owner

s-a commented Jan 9, 2016

OSOBLETE! Thanks to @michael-brade #73 (comment) we have now a very smarter way to debug Mocha!

Please refer to https://github.com/s-a/iron-node/blob/master/docs/DEBUG-NODEJS-COMMANDLINE-APPS.md for forthcoming updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants