简体   繁体   English

Jenkins中的Unit Testing Express应用

[英]Unit Testing Express app in Jenkins

Ok, I'm searching a couple of hours for a solution to run my Mocha unit tests inside Jenkins, for my Express JS app. 好的,我正在搜索几个小时的解决方案,以在Express JS应用程序的Jenkins中运行我的Mocha单元测试。

Writing tests is quite easy, but connecting the tests with my app is a bit harder. 编写测试非常容易,但是将测试与我的应用程序连接起来会有点困难。 An example of my current test: 我当前测试的一个示例:

/tests/backend/route.test.js /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();
        });
    });
});

In above example I connect to a running app (see ```var app = ' http://someurl.com '``). 在上面的示例中,我连接到正在运行的应用程序(请参阅```var app =' http ://someurl.com'``)。 Obviously this is not working inside Jenkins, except if I can tell Jenkins to first run the app and then check for a localhost url. 显然,这在Jenkins内部不起作用,除非我可以告诉Jenkins首先运行应用程序,然后检查本地主机URL。 But how to do this? 但是该怎么做呢?

Now if I take a look at https://www.npmjs.org/package/supertest this should be enough to test my express app: 现在,如果我看一下https://www.npmjs.org/package/supertest,这应该足以测试我的快速应用程序:

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

var app = express();

But it's not. 但事实并非如此。 I receive 404 errors on all urls which I would like to test. 我要测试的所有网址都收到404错误。

Anyone who knows how to test my app inside Jenkins? 有谁知道如何在Jenkins中测试我的应用程序吗?

Is the app you referenced at http://someurl.com the app you're trying to write this test for? 您在http://someurl.com引用的应用程序是您要为此测试编写的应用程序吗? If so... 如果是这样的话...

Supertest should allow you to run tests without needing an external server running. Supertest应该允许您运行测试而无需运行外部服务器。 The way you accomplish this is by separating out the server routing Express code from the code you'll run that actually starts the server, and use Supertest on the server routing code. 完成此操作的方法是,将服务器路由Express代码与实际运行服务器的运行代码分开,并对服务器路由代码使用Supertest。

Here is an example: 这是一个例子:

Given this directory structure: 给定此目录结构:

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

Here are my files: 这是我的文件:

./package.json ./package.json

All of these dependencies aren't required for this example, I just pulled this from my package.json I'm using in my project. 本示例并不需要所有这些依赖关系,我只是从我在项目中使用的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 ./server/index.js

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

./app.js ./app.js

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

./tests/server-test.js ./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);
    });
});

I'm using Mocha and Chai here (didn't have time to refactor and make the example simpler, sorry). 我在这里使用MochaChai (没有时间重构和简化示例,很抱歉)。 The main point is that within the it statement you can run your tests. 要点是,您可以在it语句中运行测试。

Doing it this way does not require a separate server running. 以这种方式执行此操作不需要运行单独的服务器。 Supertest magically starts one up for you and runs it based on the express server you exported from the server module. Supertest为您神奇地启动了一个,并根据您从服务器模块导出的Express服务器运行它。 I think this approach should allow you to run your tests seamlessly through Jenkins. 我认为这种方法应允许您通过Jenkins无缝运行测试。

In ./server/index.js , an Express object is created. ./server/index.js ,创建一个Express对象。 That object is exported, and imported via require in both ./app.js and ./test/server-test.js . 该对象将通过./app.js./test/server-test.js require导出和导入。 In ./app.js it's used to start a server via listen() . ./app.js它用于通过listen()启动服务器。 In the test file, Supetest uses it to start a server for testing internally. 在测试文件中,Supetest使用它来启动服务器以进行内部测试。

When you are running the server in this example, you would run it via ./app.js . 在此示例中运行服务器时,可以通过./app.js运行它。 That would set the port you choose (8000 in this example) and start listening for connections on that port. 这将设置您选择的端口(在本示例中为8000),并开始侦听该端口上的连接。

Hope this helps! 希望这可以帮助! Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM