【问题标题】:Unit testing nodejs module using mocha and chai使用 mocha 和 chai 对 nodejs 模块进行单元测试
【发布时间】:2021-09-14 16:05:46
【问题描述】:

我有一个 nodejs 应用程序,我正在为其编写单元测试,我遇到以下情况:我有一个 config.js 文件,我在其中读取多个参数作为环境变量和一些证书以连接到 RabbitMQ。我将此配置导出为模块。

我想做的是创建一个单元测试文件,它将在不同的场景中测试模块并尝试实现非常高的覆盖率。

我有几个环境,如 LOCAL_DEV、DEV、UAT 和 PROD,以及多个 RabbitMQ 实例。例如,我使用密钥文件连接到 RabbitMQ,在 LOCAL_DEV 中仅使用用户名和密码,在 UAT 中使用 pfx 等等。

像 process.env.ENO_ENV 这样的环境变量在部署期间被注入,其他配置参数我通过 .env_dev 或 .env_uat 文件获取它们。当我进行部署时,我还会获得注入的证书,并且它们的路径在“.env_”文件中给出。

例如,在 DEV 中,我有 process.env.ENO_ENV = dev 并且 .env_dev 看起来像:

RABBITMQ_SSL_KEYFILE = 'tls/dev/rmq-XXX.key'
RABBITMQ_SSL_CERTFILE = 'tls/dev/rmq-XXX.crt'
RABBITMQ_SSL_CAFILE = 'tls/dev/rmq-dev-ca.crt'

测试在 Gitlab 中完成,我没有提供任何证书文件或 .env_ 文件,因此我需要模拟 process.env.ENO_ENV 和 .env_file。

config.js 文件的内容:

const fs = require('fs');
const path = require('path');

// Load config for the set env
global.appRoot = path.resolve(__dirname);
const eno_env = process.env.ENO_ENV || "";

if (process.env.ENO_ENV == 'dev' || 
  process.env.ENO_ENV == 'local_dev' || 
  process.env.ENO_ENV == 'uat' || 
  process.env.ENO_ENV == 'prod') {    
  require('dotenv').config({ path: path.join(global.appRoot, '.env_' + eno_env) });
} else {
  require('dotenv').config({ path: path.join(global.appRoot, '.env') });
}

const {
  PORT,
  RABBITMQ_PROTOCOL,
  RABBITMQ_USER,
  RABBITMQ_PASS,
  RABBITMQ_HOSTS,
  RABBITMQ_PORT,
  RABBITMQ_VHOST,
  RABBITMQ_EXCHANGE,
  RABBITMQ_QUEUE,
  RABBITMQ_SSL_CAFILE,
  RABBITMQ_SSL_KEYFILE,
  RABBITMQ_SSL_CERTFILE,
  RABBITMQ_SSL_PFX,
  RABBITMQ_SSL_PASSWORD,
} = process.env;

let opts = {};

if (RABBITMQ_SSL_PFX) {
  console.log ('RABBITMQ_SSL_PFX : ' + path.join(global.appRoot, RABBITMQ_SSL_PFX));
  opts = {
    pfx: fs.readFileSync(path.join(global.appRoot, RABBITMQ_SSL_PFX)),
    passphrase: RABBITMQ_SSL_PASSWORD,
    ca: [fs.readFileSync(path.join(global.appRoot, RABBITMQ_SSL_CAFILE))],
    rejectUnauthorized: false,
  };
} else if (RABBITMQ_SSL_KEYFILE) {
  opts = {
    key: fs.readFileSync(path.join(global.appRoot, RABBITMQ_SSL_KEYFILE)),
    cert: fs.readFileSync(path.join(global.appRoot, RABBITMQ_SSL_CERTFILE)),
    ca: [fs.readFileSync(path.join(global.appRoot, RABBITMQ_SSL_CAFILE))],
    rejectUnauthorized: false,
  };
}

const config = {
  app: {
    port: parseInt(PORT) || 8126,
    enoenv: eno_env,
  },
  rabbitmq: {
    protocol: RABBITMQ_PROTOCOL || 'amqps',
    user: RABBITMQ_USER || 'test',
    pass: RABBITMQ_PASS || 'test',
    hosts: RABBITMQ_HOSTS || 'localhost',
    port: parseInt(RABBITMQ_PORT) || 5671,
    vhost: RABBITMQ_VHOST || 'test_virtualhost',
    exchange: RABBITMQ_EXCHANGE || 'test_exchange',
    queue: RABBITMQ_QUEUE || 'test_queue',
    opts,
  },
};

module.exports = config;

我的问题是通过使用名为 configCert1UnitTest.js 的文件来实现 mocha 和 chai 的单元测试覆盖率:

const should = require('chai').should();

process.env['RABBITMQ_SSL_PFX'] = '/test/test.json';
process.env['RABBITMQ_SSL_PASSWORD'] = 'test';
process.env['RABBITMQ_SSL_CAFILE'] = '/test/test.json';

const config = require('./../config');

describe('env 1', function () {
    // running tests
    it('reads env RABBITMQ_SSL_PFX property, config.rabbitmq.opts.pfx and test.json file correctly', function () {
        try {
            //target = fs.readFileSync(path.join(global.appRoot, process.env['RABBITMQ_SSL_PFX']));
            should.equal( 
                config.rabbitmq.opts.pfx.toString('utf8'), 
                '{\r\n    "property" : "test"\r\n}');
          } catch(e) {
            throw new Error("error reading env RABBITMQ_SSL_PFX property, config.rabbitmq.opts.pfx or test.json: " + e.message);
          }    
    });

    it('reads env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca and test.json file correctly', function () {
        try {
            should.equal( 
                config.rabbitmq.opts.ca.toString('utf8'), 
                '{\r\n    "property" : "test"\r\n}');
          } catch(e) {
            throw new Error("error reading env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca or test.json: " + e.message);
          }
              
    });
  });
  
  delete process.env.RABBITMQ_SSL_PFX;
  delete process.env.RABBITMQ_SSL_PASSWORD;
  delete process.env.RABBITMQ_SSL_CAFILE;

现在,由于 condif.js 中的其他 IF 被覆盖,我在文件 configCert2UnitTest.js 中编写了第二个测试用例:

const should = require('chai').should();

process.env['RABBITMQ_SSL_KEYFILE'] = '/test/test.json';
process.env['RABBITMQ_SSL_CERTFILE'] = '/test/test.json';
process.env['RABBITMQ_SSL_CAFILE'] = '/test/test.json';

const config = require('./../config');
  
describe('env 2', function () {
    
    // running tests
    it('reads env RABBITMQ_SSL_KEYFILE property, config.rabbitmq.opts.key and test.json file correctly', function () {
        try {
            should.equal( 
                config.rabbitmq.opts.key.toString('utf8'), 
                '{\r\n    "property" : "test"\r\n}');
          } catch(e) {
            throw new Error("error reading env RABBITMQ_SSL_KEYFILE property, config.rabbitmq.opts.key or test.json: " + e.message);
        }    
    });

    it('reads env RABBITMQ_SSL_CERTFILE property, config.rabbitmq.opts.cert and test.json file correctly', function () {
        try {
            should.equal( 
                config.rabbitmq.opts.cert.toString('utf8'), 
                '{\r\n    "property" : "test"\r\n}');
          } catch(e) {
            throw new Error('error reading env RABBITMQ_SSL_CERTFILE property, config.rabbitmq.opts.cert or test.json:' + e.message);
          }    
    });

    it('reads env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca and test.json file correctly', function () {
        try {
            should.equal( 
                config.rabbitmq.opts.ca.toString('utf8'), 
                '{\r\n    "property" : "test"\r\n}');
          } catch(e) {
            throw new Error('error reading env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca or test.json: ' + e.message);
          }    
    });

  });

delete  process.env.RABBITMQ_SSL_KEYFILE;
delete process.env.RABBITMQ_SSL_CERTFILE;
delete process.env.RABBITMQ_SSL_CAFILE;

问题是测试没有覆盖模块配置的ELSE。

我的 package.json 中用于测试的命令:

...
"test": "npx nyc mocha test --exit --timeout 10000 --reporter mocha-junit-reporter",
...

输出:

PS D:\zzz-api> npx mocha test
global.appRoot : D:\zzz-liveprice-api
RABBITMQ_SSL_PFX : D:\zzz-api\test\test.json


  env 1
    ✔ reads env RABBITMQ_SSL_PFX property, config.rabbitmq.opts.pfx and test.json file correctly
    ✔ reads env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca and test.json file correctly

  env 2
    1) reads env RABBITMQ_SSL_KEYFILE property, config.rabbitmq.opts.key and test.json file correctly
    2) reads env RABBITMQ_SSL_CERTFILE property, config.rabbitmq.opts.cert and test.json file correctly
    ✔ reads env RABBITMQ_SSL_CAFILE property, config.rabbitmq.opts.ca and test.json file correctly

  Dummy Test
    ✔ First test that should pass

  Bindings Unit Test
    ✔ Should return true if the correct bindings are found for the EUA
    ✔ Should return true if the correct bindings are found for the POWER
    ✔ Should return true if the correct bindings are found for the GAS


  7 passing (534ms)
  2 failing

  1) env 2
       reads env RABBITMQ_SSL_KEYFILE property, config.rabbitmq.opts.key and test.json file correctly:
     Error: error reading env RABBITMQ_SSL_KEYFILE property, config.rabbitmq.opts.key or test.json: Cannot read property 'toString' of undefined
      at Context.<anonymous> (test\configCert2UnitTest.js:19:19)
      at processImmediate (internal/timers.js:439:21)

  2) env 2
       reads env RABBITMQ_SSL_CERTFILE property, config.rabbitmq.opts.cert and test.json file correctly:
     Error: error reading env RABBITMQ_SSL_CERTFILE property, config.rabbitmq.opts.cert or test.json:Cannot read property 'toString' of undefined
      at Context.<anonymous> (test\configCert2UnitTest.js:30:19)
      at processImmediate (internal/timers.js:439:21)

我忘了提到我在我的 \test 目录中为测试设置了一个 test.json 文件,其中包含 '{\r\n "property" : "test"\r\n}' 只是为了模拟在 Gitlab 中测试时缺少的证书。

任何想法如何解决这个问题?谢谢!

【问题讨论】:

    标签: node.js unit-testing rabbitmq mocha.js chai


    【解决方案1】:

    好的,我设法通过执行以下操作解决了我的问题:

    我在我的 config.js 中创建了一个函数:

    const fs = require('fs');
    const path = require('path');
    
    module.exports = () => {
      // Load config for the set env
      global.appRoot = path.resolve(__dirname);
      const eno_env = process.env.ENO_ENV || "";
      ...
      return config;
    }
    

    然后,在我的测试文件configUnitTest.js 中,我将常量定义移到了describe() 方法中,并将其放在before() 块中,而不是在测试文件的开头使用它。

    此外,mocha 测试文件夹 test 中的其余文件,如果有另一个测试文件读取 const conf = require('../config')();,这将

    const should = require('chai').should();
    const mock = require('mock-fs');
    const path = require('path');
    
    describe('Reading config.js - environmental vars 1', function () {
      let conf;
      before('mocking files and process.env vars', function () {        
        ...
        process.env['ENO_ENV'] = 'local_dev';
        conf = require('../config')(); // reads process.env.ENO_ENV
      });
      ...
    
      // running tests  
      it('TRUE if it reads var process.env.RABBITMQ_SSL_PFX and mocked file test.json', function () {
        ...
      });
      ...
    });
    
    describe('Reading config.js - environmental vars 2', function () {
      let conf;
      before('mocking files and process.env vars', function () {        
        ...
        conf = require('../config')(); // reads const eno_env = process.env.ENO_ENV || "";
      });
      ...
    
      // running tests  
      it('TRUE if it reads var process.env.RABBITMQ_SSL_PFX and mocked file test.json', function () {
        ...
      });
      ...
    });
    

    我希望它可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2021-09-27
      • 2017-02-18
      • 2015-05-16
      相关资源
      最近更新 更多