【问题标题】:Detecting command line invocation with node.js and Mocha使用 node.js 和 Mocha 检测命令行调用
【发布时间】:2016-06-03 18:21:48
【问题描述】:

我正在尝试找到一种方法来检测文件是从命令行实例化还是在 mocha 单元测试中需要。通常一个人这样做

if (require.main === module)

但我知道在使用 Mocha 时,require.module 在 Mocha 中是一些 dir,这要归功于 this post。

为什么我需要它:我正在编写单元测试并希望选择任一

  • 在index.js 中包含所有文件并同时运行所有测试
  • 或一次运行单个文件测试。

这是我想要做的基本想法(在代码中)。有人知道如何调整此代码以获得我需要的功能吗?

index.js:

'use strict';

var chai = require('chai');
var expect = chai.expect;

var displayErrors = require("./displayErrors");

describe("Running all tests now...", function () {
    displayErrors();
});

显示错误:

"use strict";

var displayErrors = require("../../bin/displayErrors");

function test () {
    describe("Testing displayErrors.js now...", function () {
        it("blah", function (done) {
            displayErrors(function (status, statusText) {
                expect(1).to.equal(1);
                done();
            });
        });
        it("blah", function (done) {
            displayErrors(function (status, statusText) {
                expect(1).to.equal(1);
                done();
            });
        });
    });
}

module.exports = test;

if (require.main === module) {
    var chai = require('chai');
    var expect = chai.expect;

    test();
}

【问题讨论】:

    标签: node.js mocha.js


    【解决方案1】:

    在 Mocha 中,您不需要 index.js。你可以这样做:

    mocha // runs all tests
    mocha -g blah // runs only tests with 'blah' in it or description
    

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多