ITKeyword,专注技术干货聚合推荐

注册 | 登录

阿里巴巴分布式SOA框架Dubbo简介

qicong

相关推荐:阿里巴巴SOA服务化治理方案的核心框架-Dubbo

关键字:阿里巴巴SOA服务化治理方案的核心框架-Dubbo 一、简述 Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支

up vote 3 down vote favorite 2

I have an https server running by express, which i test using mocha and supertest.

My problem is - if i run only the test - its ok. If i try to run gruntfile with test then run express - i see lot of EADDRINUSE errors, even if in test files i do after() with app.close(). Same applies to watch task on tests.

Here is my exapmle test:

/* jshint node: true*/
/*global describe, it, after*/
(function() {
    'use strict';
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    var request = require('supertest');
    var app = require('../server.js').app;
    var expect = require('chai').expect;
    var Cookies;

    after(function(done) {
        app.close();
        setTimeout(function(){done();}, 1500);
    });

    describe('/login', function() {
        it('should auth the user', function(done) {
            request(app)
                .post('/login')
                .send({login: "test", password: 'test'})
                .expect(302)
                .end(function(err, res) {
                    expect(err).to.be.equal(null);
                    expect(res.text).to.be.equal("Moved Temporarily. Redirecting to /");
                    Cookies = res.headers['set-cookie'].pop().split(';')[0];
                    done();
            });

        });
    });
    // testing API for serving session data for angualar factory
    describe('/api/session', function() {
        it('should return session data in JSON', function(done) {
            var req = request(app).get('/api/session');             
                req.cookies = Cookies;
                req.set('Accept','application/json')
                .end(function(err, res) {
                    expect(err).to.be.equal(null);
                    expect(res.body).to.have.property("_id");
                    done();
            });
        });
    });

}());

Im aware that those test are far from perfect. Im just beginning my adventure with proper software testing.

All those "port already in use" are obvious, and none of them... give any issue. all tests work fine, server works fine, but stdout is crazy. Such behaviour is far from ideal, and probably full of potential problems and unstability issues.

My question is - how to get rid of it?

My ideas are:

  • create dedicated server for test on just different port. Unfortunately i have to clue how to implement this.

  • make some conditional for superagent to run server if it is not running, or just... pass it to superagent otherwise?

  • use something else then superagent (like request, but im not sure if all the cookies and node_tls_reject_unauthorized would work.

As You can see - i struggle with that topic and have more questions than answers, and not enough experiance to know where to look at.

相关推荐:阿里巴巴开源服务框架Dubbo2.0.9发布

链接:http://www.iteye.com/news/23690

I kindly appreciate any help.

EDIT:

I found, that i can do:

before(function(done) {
    app.listen(3001, function() { done(); });
});

which starts test on another port but... the whole server.js is loaded anyway, so it starts anyway too. Then, when firing it together with running server, there is obvious EADDRINUSE.

node.js unit-testing express mocha supertest share edited Jul 29 '14 at 19:55 asked Jul 29 '14 at 19:33 Jarema 1,184 7 22      Can't see your gruntfile but likely you are starting the node app, then testing it. –  AGDM Dec 16 '15 at 18:09 add a comment  | 

1 Answer 1

active oldest votes up vote 6 down vote accepted

When using superagent, you should always pass it an Express application which has been configured (middleware registered, controllers routed, etc.)--but not initialized as a HTTP server. It will do that for you and it will defer, through http.createServer, to the OS to pick an available port.

If you currently have that the server.js module is already providing a static instance of a full-blown HTTP server, that is most likely the source of your problems. In either case, try extracting the application configuration/bootstrapping from the actual server instantiation like so:

// server.js
var express = require('express');
var middleware = require('./middleware');
var controllers = require('./controllers');

// Configures the Express application instance.
exports.setup = function (app) {
    app.use(middleware.foo);
    app.get('/bar', controllers.bar);

    app.locals.baz = 'quux';
}

// You might shoot yourself in the foot if parts of your application depend
// on a static reference at `server.app`.
exports.app = setup(express());
exports.app.listen(3000);

Then, in your tests, you can do something along the lines:

// tests.js
var express = require('express');
var server = require('./server');

describe('Server tests', function () {
    // Create a fresh server instance prior to each test
    beforeEach(function createNewSever() {
        this.app = server.setup(express());
    });

    describe('Foo', function () {
        it('barrs', function () {
            request(this.app)  // initializes a server instance on port A
            // ... supertests
        });

        it('bazzes', function () {
            request(this.app)  // initializes a server instance on port B
            // ... more supertests
        });
    });
});

This is just for illustrative purposes, where the when/how of instantiating the application instance will depend on your test context. The important thing to take away is that you should be able to create fresh, clean, independent and isolated server instances for your test cases. It is an absolute necessity should you use a test runner that executes tests in parallel or in a random order.

share edited Jan 13 '15 at 13:04 answered Jul 29 '14 at 21:01 Filip Dupanović 15.7k 8 51 83      Is there no issue with server.js actually spinning up the server to listen on port 3000. Wouldn't it be better to have the setup function in a separate module? –  Robert Moskal Jan 14 '15 at 19:05 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged node.js unit-testing express mocha supertest or ask your own question.

相关推荐:阿里巴巴宣布5.4亿战略投资中国万网==互联网电子商务绑定互联网基础服务>为手机电子商务开拓铺路

  在阿里巴巴集团高调宣布未来十年战略两周之后,国内最大的电子商务提供商阿里巴巴网络有限公司(HK.1688)今日(9月28日)宣布,将支付人民币5.40亿元的现金,分

一、 Dubbo背景介绍随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,需一个治理系统确保架构有条不紊的演进 单一应用

相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。