4

Is there a way to get the filename of current test in mocha reporter?

I couldn't find anything in the base and examples.

shabunc
  • 23,119
  • 19
  • 77
  • 102
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • It's simply `runner.on('fail', test => test.filename)` (came to this after a long time and the answer wasn't immediately obvious) – laggingreflex Dec 18 '15 at 09:57

1 Answers1

4

Actually, file name is passed to Suite in file field in mocha starting from this pull request. It's just nowadays mocha most commonly is ran as a karma plugin (namely, karma-mocha plugin), and, talking of December'14, this plugin just does not pass file name information further.

To make this answer self-consistent, here's how Suite is formed in mocha (it's tdd implementation, but it it is similar for bdd):

context.suite = function(title, fn){
      var suite = Suite.create(suites[0], title);
      suite.file = file;
      suites.unshift(suite);
      fn.call(suite);
      suites.shift();
      return suite;
    };

And here's how suits are formed in karma-mocha/lib/adapter.js:

 runner.on('test end', function(test) {
      var skipped = test.pending === true;

      var result = { 
        id: '', 
        description: test.title,
        suite: [], 
        success: test.state === 'passed',
        skipped: skipped,
        time: skipped ? 0 : test.duration,
        log: test.$errors || []
      };  

      var pointer = test.parent;
      while (!pointer.root) {
        result.suite.unshift(pointer.title);
        pointer = pointer.parent;
      }   

      tc.result(result);
    });

But you know what, I guess this is a nice thing to issue as a feature request in karma-mocha project.

shabunc
  • 23,119
  • 19
  • 77
  • 102