javascript - Jenkins 中的单元测试 Express 应用程序

标签 javascript node.js express jenkins

好的,我正在搜索几个小时的解决方案,以便在 Jenkins 中为我的 Express JS 应用程序运行 Mocha 单元测试。

编写测试非常简单,但将测试与我的应用程序连接起来有点困难。我当前测试的一个例子:

/tests/backend/route.test.js

var should    = require('should'),
    assert    = require('assert'),
    request   = require('supertest'),
    express   = require('express');

describe('Routing', function() {

  var app = 'http://someurl.com';

  describe('Account', function() {
    it('should return error trying to save duplicate username', function(done) {
      var profile = {
        username: 'vgheri',
        password: 'test',
        firstName: 'Valerio',
        lastName: 'Gheri'
      };
    // once we have specified the info we want to send to the server via POST verb,
    // we need to actually perform the action on the resource, in this case we want to 
    // POST on /api/profiles and we want to send some info
    // We do this using the request object, requiring supertest!
    request(app)
    .post('/api/profiles')
    .send(profile)
    // end handles the response
    .end(function(err, res) {
          if (err) {
            throw err;
          }
          // this is should.js syntax, very clear
          res.should.have.status(400);
          done();
        });
    });
});

在上面的示例中,我连接到正在运行的应用程序(请参阅``var app = ' http://someurl.com '``)。显然,这在 Jenkins 内部不起作用,除非我可以告诉 Jenkins 首先运行该应用程序,然后检查 localhost url。但如何做到这一点呢?

现在,如果我看一下 https://www.npmjs.org/package/supertest这应该足以测试我的 Express 应用程序:

var request = require('supertest')
  , express = require('express');

var app = express();

但事实并非如此。我在想要测试的所有网址上都收到 404 错误。

有人知道如何在 Jenkins 中测试我的应用程序吗?

最佳答案

您在 http://someurl.com 引用的应用程序是您尝试为其编写此测试的应用程序吗?如果是这样的话...

Supertest 应该允许您在不需要运行外部服务器的情况下运行测试。实现此目的的方法是将服务器路由 Express 代码与实际启动服务器的代码分开,并在服务器路由代码上使用 Supertest。

这是一个例子:

鉴于此目录结构:

./package.json
./server/index.js
./app.js
./test/server-test.js

这是我的文件:

./package.json

此示例不需要所有这些依赖项,我只是从我在项目中使用的 package.json 中提取了它。

{
  "name": "StackOverflowExample",
  "version": "1.0.0",
  "description": "Example for stackoverflow",
  "main": "app.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "Mark",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.2",
    "ejs": "^2.5.2",
    "express": "^4.14.0",
    "express-validator": "^2.21.0",
  },
  "devDependencies": {
    "chai": "^3.5.0",
    "mocha": "^3.1.2",
    "supertest": "^2.0.1"
  }
}

./server/index.js

var express = require('express');
var app = express();
app.get('/', function(req, res) {
    res.send('Hello World');
});
module.exports = app;

./app.js

var app = require('./server');
var server = app.listen(8000, function() {
    console.log('Listening on port 8000');
});

./tests/server-test.js

var request = require('supertest');
var app = require('../server');

var assert = require('assert'); //mocha
var chai = require('chai');
var expect = null;

chai.should();
expect = chai.expect;

describe('GET /', function() {
    it('should return a 200', function(done) {
        request(app).get('/').expect(200, done);
    });
});

我正在使用 MochaChai这里(抱歉,没有时间重构并使示例更简单)。要点是在 it 语句中您可以运行测试。

这样做不需要运行单独的服务器。 Supertest 会神奇地为您启动一个并根据您从服务器模块导出的 Express 服务器运行它。我认为这种方法应该允许您通过 Jenkins 无缝地运行测试。

./server/index.js中,创建了一个Express对象。该对象通过 ./app.js./test/server-test.js 中的 require 导出和导入。在 ./app.js 中,它用于通过 listen() 启动服务器。在测试文件中,Supetest使用它来启动服务器进行内部测试。

当您在此示例中运行服务器时,您将通过 ./app.js 运行它。这将设置您选择的端口(本例中为 8000)并开始监听该端口上的连接。

希望这有帮助!祝你好运!

关于javascript - Jenkins 中的单元测试 Express 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036985/

相关文章:

node.js - Express body-parser 不解析多个路由器中间件请求

javascript - 如何按值对 Mongoose 填充的数组进行排序?

angularjs - Nodejs Express 渲染不适用于 ejs

node.js - NodeJS : if I return mysql data to client in the end of request does it mean that having callbacks doesn't make sense?

javascript - 如何在 Dojo 中保存过滤器

javascript - 从 Django 到 JavaScript 的全局变量

javascript - 防止客户重复提交订单?

node.js - Node 邮件程序错误

javascript - 为什么我的函数在 NodeJS、Sequelize 中返回 null

javascript - 可以在下划线模板中访问 'root' 模型吗?