Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha - how do I list files that will be executed

Tags:

mocha.js

I'm wondering if there is a way to get mocha to list all the tests that it will execute. I don't see any reasonable options when I list them using mocha --help; There are several reporters, but none appear designed to list the files that will be processed (or name the tests that will be run).

like image 546
RoyM Avatar asked Sep 28 '22 01:09

RoyM


1 Answers

The way reporters work is by listening for events dispatched by mocha, these events are only dispatched when running a real test.

A suite contains a list of tests, so there is the info that you need. However, the suite is usually only initialized on run().

If you are ok with running mocha from nodejs instead of from the command line you can create this diy solution based on the code from Using-mocha-programmatically:

var Mocha = require('mocha'),
    fs = require('fs'),
    path = require('path');

// First, you need to instantiate a Mocha instance.
var mocha = new Mocha(),
    testdir = 'test';

// Then, you need to use the method "addFile" on the mocha
// object for each file.

// Here is an example:
fs.readdirSync(testdir).filter(function(file){
    // Only keep the .js files
    return file.substr(-3) === '.js';

}).forEach(function(file){
    // Use the method "addFile" to add the file to mocha
    mocha.addFile(
        path.join(testdir, file)
    );
});

// Here is the code to list tests without running:

// call mocha to load the files (and scan them for tests)
mocha.loadFiles(function () {
    // upon completion list the tests found
    var count = 0;
    mocha.suite.eachTest(function (t) {
        count += 1;
        console.log('found test (' + count + ') ' + t.title);
    })
    console.log('done');
});
like image 63
Simon Groenewolt Avatar answered Oct 04 '22 12:10

Simon Groenewolt