nathanboktae / mocha-casperjs

Write CasperJS tests using Mocha
MIT License
120 stars 29 forks source link

Reporter that wraps folders in describe blocks #53

Closed thekevinscott closed 9 years ago

thekevinscott commented 9 years ago

Hi, sorry to use the issues here to ask a question but I'm a bit stumped and hoping someone might be able to help.

I'm trying to write a third party reporter for Mocha. Our tests are organized in folders like:

/users/create.js
/users/edit.js
/groups/create.js
/groups/edit.js

Which we specify as a file array in a grunt script.

What I'm hoping to see in the output is something like:

Users
  Create
    should foo
  Edit
    should bar

Groups
  Create
    should foo
  Edit
    should bar

Where create has a single describe block, "Create", and a single test, "should foo".

I can achieve what I'm asking for by modifying cli.js like so:

var rootDir = require('fs').workingDirectory;
tests.map(function(test) {
  return fs.absolute(test).replace('.coffee', '').replace('.js', '')
}).forEach(function(test) {
  var folder = test.substring(rootDir.length).split('/');
  if ( folder.length > 1 ) {
    describe(folder[0], function() {
      require(test)
    });
  } else {
      require(test)
  }
})

Obviously this wouldn't work for tests nested multiple levels deep, but this basically does what I'm looking to achieve.

Because tests are required in the cli.js script the suites in the reporter have no access to their parent file. Is there any way to achieve what I'm asking that wouldn't require modifying cli.js? I'm kinda stumped.

nathanboktae commented 9 years ago

Hi, sorry to use the issues here to ask a question

Don't be - usability issues are important.

Loading tests is not the responsibility of the reporter. You can have this boot-straping logic as the one "test" that is passed into mocha-casperjs, and then choose any reporter you wish.

As for the logic, I think it can work. If a straight up require doesn't register the tests under the describe block as you think, you can expose the tests via an object and have this boot-strapper register it. it won't look as pretty that way, and then nesting inside that wouldn't work.... hopefully it works as you coded it.

thekevinscott commented 9 years ago

That's a good idea I hadn't considered, thanks Nathan. I'll give that a shot.