在特定子模块中进行摩卡测试?

Make mocha test in particular submodules?

我的应用程序包含许多较小的模块,位于 node_modules/submodulename 下的私有 npm 模块中。如何让 mocha 调用特定子文件夹中的测试?

我已经关注了 https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically,但它似乎不起作用:

var log = console.log.bind(console)

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

var testModule = function(moduleName){
    var dirName = 'node_modules/'+moduleName

    // Instantiate a Mocha instance.
    var mocha = new Mocha({
        ui: 'tdd'
    });

    // Add each .js file to mocha
    fs.readdirSync(dirName).filter(function(file){
        // Only keep the .js files
        return file.substr(-3) === '.js';
    }).forEach(function(file){
        log('Adding', file)
        mocha.addFile(
            path.join(dirName, file)
        );
    });

    // Run the tests.
    mocha.run(function(failures){
        process.on('exit', function () {
            process.exit(failures);
        });
    });
}

testModule('mycompany-blog')

mocha.addFile()使用的目录必须明确'test'目录,而不是包含测试目录的目录:

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

var testModule = function(moduleName){
    var dirName = 'node_modules/'+moduleName+'/test'

    // Instantiate a Mocha instance.
    var mocha = new Mocha({
        ui: 'tdd'
    });

    // Add each .js file to mocha
    fs.readdirSync(dirName).filter(function(file){
        // Only keep the .js files
        return file.substr(-3) === '.js';
    }).forEach(function(file){
        log('Adding', file)
        mocha.addFile(
            path.join(dirName, file)
        );
    });

    // Run the tests.
    mocha.run(function(failures){
        process.on('exit', function () {
            process.exit(failures);
        });
    });
}

我已经更新 https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically 以反映这一点。

您不得将本地模块放在 node_modules/ 中。该文件夹不会进入源代码管理。

你可以做的是创建一个文件夹 privmod1 并将测试放入 privmod1/test 然后 cd privmod1 和 运行 mocha。您想单独测试该模块。

或者,您可以 require/import 来自测试文件中的父目录,例如 require('../privmod1')

私有模块的其他选项:只需将它们放在 github 您可以访问并引用 githubuser/privmod1 的存储库中,或者使用官方的 npm 私有模块系统。