connect ECONMREFUSED when using MOCHA to SUPERTEST an express app - node.js

I am using MOCHA to test some express framework code.
I have written a simple MOCHA code to test messages returned in the response header. The code works. It also means that I am connected to the server and I can get the file from the database.
Now, I want to use "SuperTest" to do the same thing. But, I get "Error: connect ECONMREFUSED"
Here is my code:
var express = require('express');
var request = require('supertest');
var app = express();
describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
this.timeout(15000);
it ('Check header message', function(done) {
request(app)
.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
.expect('warning', '100 Max Record Limit Exceeded')
.expect('Content-Type', /json/)
.expect(200, done);
} )
} )
and the error showing on the console is:
1) GET /core/dbq/534e930204dd311822ec1c9d Check header message:
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
I am learning to use "SuperTest". Please help. Thank you.

Supertest starts the application on a random port and fills the host+port part of the URL for you. Your code should supply the path (and query) part only.
request(app)
.get('/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
// etc.
.expect(200, done);
Alternatively, you can start the application yourself before running the test.
describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
this.timeout(15000);
before(function(done) {
app.listen(3001, function() { done(); });
});
it ('Check header message', function(done) {
request(app)
.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
// etc.
});
});
I would highly recommend going with the first approach, otherwise your tests may clash with other applications listening on port 3001.

Related

Unit testing Express / Loopback middleware without starting a server

Is there a way to unit test Express / Loopback middleware without actually creating a server and listening on a port?
The problem I have is that creating multiple servers in my test code will introduce the problem of port conflicts.
You can use the supertest module.
You may pass an http.Server, or a Function to request() - if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports.
Mocha
An example with mocha
var request = require('supertest');
var app = require('path/to/server.js');
describe('GET /user', function() {
it('respond with json', function(done) {
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
Ava
Also, you might be interested in the ava test runner instead of mocha.
Main reason : process isolation between test files
This is usually my test file template with ava
var describe = require('ava-spec').describe;
var app = require('path/to/server.js');
var request = require('supertest');
describe("REST API", function(it){
it.before.cb(function(t){
request(app)
.post('/api/Clients/')
.send({
"username": "foo",
"password": "bar"
})
.expect(200, function(err, res){
t.end(err);
});
});
it.serial.cb('Does something',
function(t){
request(app)
.get(//..)
.expect(404, function(err, res){
if (err) return t.end(err);
t.end();
});
});
it.serial.cb('Does something else afterward',
function(t){
request(app)
.get(//..)
.expect(401, function(err, res){
if (err) return t.end(err);
t.end();
});
});
});
The serial identifier tells ava to run the it clause serially. Otherwise it will run all tests from all files in parallel.
Thanks to process isolation, each test file gets its own, isolated loopback instance (and node environment in general), and all test files can be run in parallel, which speeds up tests as well. Inside each test file however, using serial, tests will run one after another, in the order they are written in the file.

Node.js TypeError: connect ECONNREFUSED 127.0.0.1:3000

I am trying to use zombie.js to log into a site but I keep on getting this error.
TypeError: connect ECONNREFUSED 127.0.0.1:3000
I think that it has something to do with the websites security but I'm not for sure. Here is the code that I am using.
const Browser = require('zombie');
Browser.localhost('test.com', 3000);
describe('User visits signup page', function() {
const browser = new Browser();
before(function(done) {
browser.visit('/', done);
});
describe('submits form', function() {
before(function(done) {
browser
.fill('Username', '*******')
.fill('password', '*******')
.pressButton('Submit', done);
});
it('should be successful', function() {
browser.assert.success();
});
it('should see welcome page', function() {
browser.assert.text('title', 'Welcome To Brains Depot');
});
});
});
This problem threw me for a bit of a loop, because my tests would run fine on one machine, but not on the other. By providence, the working machine had my application running with nodemon in the background. I didn't realize zombie doesn't start a test server for you. The non-working machine didn't have a server running, so the tests failed. To fix your problem, you could include something like this in your test file or setup:
const app = require('../../app');
const http = require('http').createServer(app).listen(3000);
If the server's running the tests should also run without crashing.

How to test app connection with express, nodeJS and Mocha

I've got my app properly configured, such that if I run node app.js everything starts up. I'm trying to test for the connection of my app is working properly using unit tests. What I have so far is:
var options = {
url: 'http://localhost',
port: 8080,
};
var app = require("../app.js");
var should = require('should');
var assert = require("assert");
var async = require('async');
it ('tests the ok connection of the app by checking that it is listening on port 8080', function(dont) {
request(options,app)
.get('/')
.expect(200)
.end(function (err, res) {
res.header['location'].should.include('/')
res.text.should.include('Listening on port 8080');
done();
});
});
I was getting the error message Error: options.uri is a required argument, which is why I added the var options. Now I'm getting error message TypeError: Object #<Request> has no method 'get'.
How can properly check that I have a good connection (200), that i'm on the right page ('/') and that it is logging the message Listening on port 8080 upon connection?
Thanks!
For the TypeError, make sure you are using supertest and not something else like request. The reason for this is that the module request does not provide the .expect() and other functions.

Issues using BusterJS, ExpressJS, and SuperTest

I'm having some issues testing routes using ExpressJS + BusterJS + SuperTest.
var app = require("../../app/app.js"),
buster = require("buster"),
expect = buster.referee.expect,
http = require('http'),
request = require('supertest');
buster.spec.expose();
describe("V2 API - group/get", function () {
var server;
beforeEach(function() {
server = http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
});
it("is accessable", function() {
request(server)
.get('/')
.expect(200)
.end(function(err, res){
server.close();
expect(err).toBe(null);
});
});
});
When I run this test, I get:
Failure: V2 API - group/get is accessible
No assertions!
1 test, 0 assertions, 1 runtime ... 1 failure
Express server listening on port 3000
Which seems wrong, because I actually do have an assertion. The problem is that it doesn't get called unless there is an error.
Another issue is that if I have multiple 'if' blocks, the server doesn't restart between them. I might be using the node + express + buster + supertest stack wrong, so any help with how to test these routes would be greatly appreciated.
I have some code that doesn't have your problem; it does almost the same thing as yours but with asynchronous tests, e.g.
it("is accessable", function(done) {
request(server)
.get('/')
.expect(200)
.end(function(err, res){
server.close();
expect(err).toBe(null);
done();
});
});
I don't know enough about Buster to know if this is the "right way" to fix this issue, but hope it helps!

How to write post request to node js server using Mocha and what are the js Needed for that

I am new to Mocha and Chai testing Framework ,i am referring the tutorial here i Understand it and its very Good for beginners but What this tutorial is it get the requests via url and in my condition i want to post and pick those data in my node server i cannot found anything unitl now so help me what are the needs and what are files to install in npm. And please send me the useful Links for Beginners Tutorials. And if may a sample application with node and mocha post requests..
We can also use the chai-http for post requests in Mocha
below is My Solution
var chai = require('chai'), chaiHttp = require('chai-http');
chai.use(chaiHttp);
var app = 'localhost:3000';
describe("Sample Unit Testing", function() {
describe("Get User Data", function() {
it("get The User Employee ID", function(done) {
// Send some Form Data
chai.request(app)
.post('/getUserData')
.send({
password: '3333',
empId: '1111'
})
.end(function (err, res) {
expect(res.EmpId).to.equal("1111");
done();
});
});
});
});
This is the Code I am Looking For and I found out myself
//you must install these two in your node js using npm
var expect = require("chai").expect;
var request = require('superagent');
describe("Name For The Test Suite", function() {
it("Testing Post Request using Mocha", function(done) {
request.post('localhost:8081/yourRequestCatcherName')
.set('Content-Type', 'application/json')
.send('{"fieldName":"data"}')
.end(function(err,res){
//your code to Test
done();
})
});
});
it Works Perfectly in the way What I want.

Resources