Question

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

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top