代码之家  ›  专栏  ›  技术社区  ›  Jürgen Steinblock

使用来自mocha测试套件的参数调用index.js

  •  0
  • Jürgen Steinblock  · 技术社区  · 6 年前

    我有一个 index.js 这需要一些论证。

    // parameters
    var outFile = process.argv[2] || (() => {throw "missing argument outFile";})();
    var templateName = process.argv[3] || (() => {throw "missing argument templateName";})();
    

    现在,我想用参数测试调用index.js,而不是测试函数本身,而是对参数进行验证。

    有办法写这样的摩卡套房

    var assert = require('assert');
    describe('Wenn calling index.js', function() {
      describe('with arguments arg1 arg2', function() {
        it('should should fail because of "missing argument outFile"', function() {
           ...
        });
      });
    });
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Jürgen Steinblock    6 年前

    process 是nodejs应用程序中的全局变量,因此您应该能够在测试中设置所需的参数。你可以重置 process.argv 使用 afterEach 钩子。

    var assert = require('assert');
    describe('Wenn calling index.js', function() {
      describe('with arguments arg1 arg2', function() {
    
        afterEach(function(){
          process.argv = process.argv.slice(0,2);
        });
    
        it('should should fail because of "missing argument outFile"', function() {
          process.argv[3] = "param templateName";
          require("path/to/index.js");
        });
      });
    });
    
    推荐文章