mocha-casperjs icon indicating copy to clipboard operation
mocha-casperjs copied to clipboard

CLI should include subdirectories

Open vlucas opened this issue 10 years ago • 8 comments

Currently, there is no way for me to run tests in subdirectories, or to even specify a directory to run tests in. I was expecting to be able to do something like mocha-casperjs tests/mysubdirectory, but it errors with CasperError: Can't find module [blah blah blah].

Running tests with the mocha-casperjs command by itself just outputs 0 passing (0ms). This doesn't allow me to organize my test suite at all, and is contrary to the behavior I would expect.

I recommend allowing a parameter that would be EITHER a single file to run, OR a directory of files to run.

vlucas avatar Apr 22 '14 20:04 vlucas

Yes I want to enable this. For now you will have to specify all tests individually.

nathanboktae avatar Apr 23 '14 01:04 nathanboktae

Yikes, this is sort of a deal-breaker. The README implies the behavior is the same as mocha, e.g. I can pass the --recursive flag and see the same behavior. What's

I'd naively assume if you can handle all tests in a test directory, you could the same recursively using something like recursive-readdir. Maybe it's not that simple? I might be able to find time to submit a patch if it's relatively straightforward.

thom-nic avatar Mar 02 '17 16:03 thom-nic

Yikes, this is sort of a deal-breaker.

Three workarounds:

  1. Simply put all your tests in a single, flat directory
  2. Use a shell script to enumerate directories and run those.
  3. Use a script that requires all the other tests as you choose.

using something like recursive-readdir.

Phantomjs is not Node.js as I've also pointed out in the readme.

nathanboktae avatar Mar 02 '17 17:03 nathanboktae

Phantomjs is not Node.js as I've also pointed out in the readme.

Thanks, I am just starting to figure that out now. Missed that in the README because I skipped the section with the heading "third party reporters" because that was not what I was looking for.

Use a script that requires all the other tests as you choose.

This seems palatable - is there a way to enumerate all files in a directory using Phantom's API? So I can do a programmatic require() in a loop without having to explicitly list each and every file. The link in that section of mocha-chaijs readme to PhantomJS's built in modules points to a non-existent wiki page. I don't see anything on this page that might provide such a capability either :/

thom-nic avatar Mar 02 '17 19:03 thom-nic

Looks like this is the URL that should be used in the readme. I'm reading through the fs module now and I'll post something once I get it working.

thom-nic avatar Mar 02 '17 19:03 thom-nic

This seems to work. in test/index.js:

const fs = require('fs'); // http://phantomjs.org/api/fs/

/**
 * require all files in this directory (does not filter js only!) and
 * recursively require any subdirectories.
 */ 
function requireDir(dir) {
  casper.log('require dir ' + dir);
  const items = fs.list(dir);
  for (var i in items) {
    const item = items[i];
    //casper.log('i ' + i + ' ' + item);
    if ( item === '.' || item === '..' ) continue;
    const path = dir + '/' + item;
    if ( fs.isDirectory(path) ) {
      requireDir(path);
    }
    else if ( fs.isFile(path) ) {
      casper.log('require '+ path);
      require(path);
    }
  }
}

// require all files/ directories under this dir.
// module.dirname is a phantomJS-specific API
requireDir(module.dirname); 

It seems something similar could be integrated into cli.js and triggered on a --recursive flag?

thom-nic avatar Mar 02 '17 20:03 thom-nic

Yeah that looks fine! Please put up a PR with a simple test too.

nathanboktae avatar Mar 06 '17 21:03 nathanboktae

Thanks I'll try but I've moved on from this project in part due to #100 and some related phantomjs issues. Will see if I have a chance to throw together a quick PR.

thom-nic avatar Mar 07 '17 13:03 thom-nic