I want to run tests on my node express server however this application is starting the server like this:
createServer()
.then(server => {
server.listen(PORT);
Log.info(`Server started on http://localhost:${PORT}`);
})
.catch(err => {
Log.error("Startup failure.", { error: err });
process.exit(1);
});
and I know the chai.request() needs to have a parameter that is pointing toward the server app, how can I export/import this createServer() function and pass it in the request method of the chai object?
You can use require.main to distinguish between your files being run directly by Node or imported as modules.
When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.
E.g.
server.js:
const express = require('express');
const PORT = 3000;
async function createServer() {
const app = express();
app.get('/', (req, res) => {
res.send('hello world');
});
return app;
}
if (require.main === module) {
createServer()
.then((server) => {
server.listen(PORT);
console.info(`Server started on http://localhost:${PORT}`);
})
.catch((err) => {
console.error('Startup failure.', { error: err });
process.exit(1);
});
}
module.exports = { createServer };
When you import the createServer from the server.js file, the if statement block will not execute. Because you want to create the server instance in the test case.
server.test.js:
const { createServer } = require('./server');
const chai = require('chai');
const chaiHTTP = require('chai-http');
const { expect } = require('chai');
chai.use(chaiHTTP);
describe('70284767', () => {
it('should pass', async () => {
const server = await createServer();
chai
.request(server)
.get('/')
.end((err, res) => {
expect(err).to.be.null;
expect(res.text).to.be.equal('hello world');
});
});
});
Related
I'm a new learner express.js I want to test simple post and get operations with tdd mechanism. I created the test, route, index and db files but when I try to test POST method it gives me this error.
This is my routes/task.js
const express = require('express');
const router = express.Router();
router.post("/api/task", async (req,res) => {
try {
const task = await new Task(req.body).save();
res.send(task);
} catch (error) {
res.send(error);
}
})
This is my test/task.js
let chai = require("chai");
const chaiHttp = require("chai-http");
const { send } = require("process");
let server = require("../index");
//Assertion Style
chai.should();
chai.use(chaiHttp);
describe('Tasks API', () => {
/**
* Test the POST Route
*/
describe('POST /api/task', () => {
it("It should POST a new task", () => {
const task = {task: "Wake Up"};
chai.request(server)
.post("/api/task")
.send(task)
.end((err, response) => {
response.should.have.status(201);
response.body.should.be.a('string');
response.body.should.have.property('id');
response.body.should.have.property('task');
response.body.should.have.property('task').eq("Wake Up");
response.body.length.should.be.eq(1);
done();
});
});
});
});
This is my db.js
var sqlite3 = require('sqlite3').verbose()
const DBSOURCE = "db.sqlite"
let db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
// Cannot open database
console.error(err.message)
throw err
}else{
console.log('Connected to the SQLite database.')
db.run(`CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task text
)`,
(err) => {
if (err) {
// Table already created
console.log(err);
}
});
}
});
module.exports = db
And this is my index.js
const connection = require('./db');
const express = require('express');
const app = express();
const cors = require("cors");
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello World');
});
app.post('/api/task', (req, res) => {
res.status(201).send(req);
});
app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports = app;
The thing that I try to do is building a test case to test the post method. I think I couldn't built the correct relations the files.
Currently, just by doing a POST request to /api/task, the error will appear. That is because of these lines in index.js:
app.post('/api/task', (req, res) => {
res.status(201).send(req);
});
The req parameter is circular, hence cannot be JSON-stringified.
Solution
In routes/task.js export the router:
const express = require('express');
const router = express.Router();
router.post("/api/task", async (req,res) => {
try {
const task = await new Task(req.body).save();
res.send(task);
} catch (error) {
res.send(error);
}
})
// By adding this line you can export the router
module.exports = router
In index.js, include the routes/task.js file and pass it to app.use(...), also remove the now-obsolete /api/task route:
const connection = require('./db');
const express = require('express');
const app = express();
const cors = require("cors");
const taskRoutes = require("./routes/task")
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello World');
});
app.use(taskRoutes)
app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports = app;
This way we got rid of the circular structure stringifying and the tests should now pass.
I was trying to get a basic hang of writing the test cases using mocha and chai-http ,I had written the test case as below
let chai = require('chai');
let chaiHttp = require('chai-http');
const should = chai.should;
const expect = chai.expect;
const server = "http://127.0.0.1:3000"
chai.use(chaiHttp);
describe('Create Login and Register', () => {
it('should login using credentials', (done) => {
chai.request(server)
.get('/register')
.send()
.then((res: any) => {
res.should.have.status(200);
done();
}).catch((err: any) => { done(err) })
})
})
and the service that i'm trying to test is as below
const express = require('express');
const app = express();
app.get('/register', function (req, res) {
res.json({
'state': true,
'msg': 'Register endpoint',
'data': {
'username': 'Swarup',
'email': 'abc#gmail.com',
'password': 'P#1234',
'fullName': 'Swarup Default'
}
});
});
app.listen(3000, () => { console.log('started') })
module.exports = app;
but when i run the test case i get an error as given below
1 failing
1) Create Login and Register
should login using credentials:
Error: connect ECONNREFUSED 127.0.0.1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
what is that i'm missing or doing wrong ?
You did not start the HTTP server. You should start the HTTP server in the before hook and teardown it in the after hook.
Besides, you can have your module NOT execute the code in the conditional block at requiring by using require.main === module condition. Because we will require('./app') in our test file, we don't want to start the HTTP server at requiring.
E.g.
app.js:
const express = require('express');
const app = express();
app.get('/register', function (req, res) {
res.json({
state: true,
msg: 'Register endpoint',
data: {
username: 'Swarup',
email: 'abc#gmail.com',
password: 'P#1234',
fullName: 'Swarup Default',
},
});
});
if (require.main === module) {
app.listen(3000, () => {
console.log('started');
});
}
module.exports = app;
app.test.js:
let chai = require('chai');
let chaiHttp = require('chai-http');
let app = require('./app');
const expect = chai.expect;
const endpoint = 'http://127.0.0.1:3000';
chai.use(chaiHttp);
describe('Create Login and Register', () => {
let server;
before(() => {
server = app.listen(3000, () => {
console.log('started for testing');
});
});
after(() => {
server.close();
});
it('should login using credentials', (done) => {
chai
.request(endpoint)
.get('/register')
.send()
.then((res) => {
expect(res).to.have.status(200);
done();
})
.catch((err) => {
done(err);
});
});
});
test result:
Create Login and Register
started for testing
✓ should login using credentials
1 passing (18ms)
I'm trying to stub auth.session when testing endpoint /allowUser2 on an express server app.js.
//--auth.js--
module.exports.session = (req, res, next) => {
req.user = null;
next();
};
//--app.js--
const express = require('express');
const auth = require('./auth');
const app = express();
app.use(auth.session);
app.get('/allowUser2', (req, res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.listen(4001).on('listening', () => {
console.log(`HTTP server listening on port 4001`);
});
module.exports = app;
If I just have this one test file test1.js in my test suite, auth gets stubbed successfully.
//--test1.js--
let app;
const sinon = require('sinon');
const auth = require('../../auth.js');
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let agent;
describe('should allow access', () => {
before(async () => {
// delete require.cache[require.resolve('../../app.js')]; // causes Error: listen EADDRINUSE: address already in use
sinon.stub(auth, 'session').callsFake((req, res, next) => {
req.user = { user: 1 };
next();
});
app = require('../../app.js');
agent = chai.request.agent(app);
});
after(async () => {
auth.session.restore();
});
it('should not allow access', async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(200);
});
});
However, if I have more than one test file that requires app.js then I have a problem. If app.js was already required in another test file, such as test2.js below, node doesn't reload app.js when it's required again in test1.js. This causes app.js to call the old auth.session function, not the new stubbed one. So the user isn't authenticated and the test fails.
//--test2.js--
const chai = require('chai');
const chaiHttp = require('chai-http');
const app = require('../../app.js');
const { expect } = chai;
chai.use(chaiHttp);
const agent = chai.request.agent(app);
describe('route /allowUser2', () => {
it("shouldn't allow access", async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(401);
});
});
I tried to reload the app.js by using delete require.cache[require.resolve('../../app.js')];. This worked when reloading a file with a plain function, but when the file is a server like app.js this causes an error: Error: listen EADDRINUSE: address already in use.
Recreate:
download Repo
npm i
npm test
How do you stub a function on the server?
One solution is turn app.js into a function that starts the server on a port number passed in as an argument. Then change the port randomly when requiring. I do not like this option because there may be some reason to keep the app on a specific port.
app.js
const express = require('express');
const auth = require('./auth');
module.exports = (port) => {
const app = express();
app.use(auth.session);
app.get('/allowUser2', (req, res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.listen(port).on('listening', () => {
console.log(`HTTP server listening on port ${port}`);
});
return app;
};
when requiring
app = require('../../app.js')((Math.random() * 10000).toString().slice(0, 4));
Instead of exporting the app in app.js, I export a function that launches the server and returns the server instance and app. By exporting the server instance I have the ability to close the server. The app is needed to pass into chai. Make sure const app = express(); is in this function and not before it or it won't recreate.
const express = require('express');
const auth = require('./auth');
const port = 4000;
module.exports = () => {
const app = express();
app.use(auth.session);
app.get('/allowUser2', (req, res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
app.post('/allowUser2', (req, res) => {
if (!req.user) return res.status(401).send();
if (req.user.user === 2) return res.status(200).send();
});
return {
server: app.listen(port).on('listening', () => {
console.log(`HTTP server listening on port ${port}`);
}),
app,
};
};
Then in my tests I can launch the server in before and close the server in after in both tests.
let app;
const sinon = require('sinon');
const auth = require('../../auth.js');
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let server;
describe('route /allowUser2', () => {
before(async () => {
// delete require.cache[require.resolve('../../app.js')]; // causes an error: `Error: listen EADDRINUSE: address already in use`.
sinon.stub(auth, 'session').callsFake((req, res, next) => {
req.user = { user: 2 };
next();
});
server = require('../../app.js')();
agent = chai.request.agent(server.app);
});
after(async () => {
server.server.close(() => {
console.log('Http server closed.');
});
auth.session.restore();
});
it('should allow access', async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(200);
});
});
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expect } = chai;
chai.use(chaiHttp);
let server;
let agent;
describe('route /allowUser2', () => {
before(async () => {
server = require('../../app.js')();
agent = chai.request.agent(server.app);
});
after(async () => {
server.server.close(() => {
console.log('Http server closed.');
});
});
it("shouldn't allow access", async function () {
const response = await agent.get('/allowUser2');
expect(response.status).to.be.equal(401);
});
});
working repo
UPDATE: Proposed Solution https://github.com/DashBarkHuss/mocha_stub_server/pull/1
One problem is the way you are using a direct method reference in app.js prevents Sinon from working. https://gist.github.com/corlaez/12382f97b706c964c24c6e70b45a4991
The other problem (address in use) is because each time we want to get a reference to app, we are trying to create a server in the same port. Breaking that app/server creation into a separate step alleviates that issue.
Am new to jest, node and express, and am having a problem with testing my app.
The actual code seems to be working - it's just when passing the server instance to each of the test files (user.test.js and genres.test.js) and running jest, the port is being blocked. I assume it's because I'm creating duplicate instances of the server in each test file, without realising it.
Running jest with the flag --runInBand works, and so does only using one test file, but this doesn't help me understand exactly what's going on.
I've posted example code below, but I'm struggling to cut it down, however I do think most of the code is irrelevant, and it's just down to how I'm passing the server to each file.
Again, apologies for the length, but I think it should be very basic for anyone but me! Thanks.
index.js (.)
const express = require('express');
const app = express();
const genres = require('./routes/genres');
const users = require('./routes/users');
app.use(express.json());
app.use('/api/genres', genres);
app.use('/api/users', users);
const port = process.env.PORT || 3000;
const server = app.listen(port, () => console.log(`Listening on port ${port}...`));
console.log(typeof server);
// export server to be used in test file
module.exports = server;
genres.js (./routes)
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
res.send('post genre ok');
});
module.exports = router;
users.js (./routes)
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
res.send('post user ok');
});
module.exports = router;
genres.test.js (./tests)
const request = require('supertest');
let server;
describe('auth tests', () => {
const exec = async function(){
return await request(server)
.post('/api/genres');
};
beforeEach(() => {
server = require('../index');
});
afterEach(async () => {
await server.close();
});
describe('POST /', () => {
it('should return 200', async () => {
const res = await request(server).post('/api/genres');
expect(res.status).toBe(200);
});
});
});
user.test.js (./tests)
const request = require('supertest');
let server;
describe('user tests', () => {
const exec = async function(){
return await request(server)
.post('/api/user');
};
beforeEach(() => {
server = require('../index');
});
afterEach(async () => {
await server.close();
});
describe('POST /', () => {
it('should return 200', async () => {
const res = await request(server).post('/api/users');
expect(res.status).toBe(200);
});
});
});
Hopefully this (point 2) helps others with this question
It has worked for me, by splitting the app from the server. I'm not sure if this is the right approach, and I'm not 100% sure why it works with the app rather than the server, but all my tests are now passing.
index.js is now app.js:
const express = require('express');
const app = express();
const genres = require('./routes/genres');
const users = require('./routes/users');
app.use(express.json());
app.use('/api/genres', genres);
app.use('/api/users', users);
// export server to be used in test file
module.exports = app;
The server is separated into another file:
const app = require('./app');
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
and then the test files import the app rather than the server. therefore each test doesn't create its own instance.
NB: I think - I don't really know how correct this is, but as mentioned, it works
I have httpListener.ts, which looks like this:
export function startListening() {
const app = express();
app
.use(bodyParser.json())
.post('/home/about', func1)
.get('/user/product/:id', func2)
.use(function (req, res) {
res.status(404).send(`no routing for path ${req.url}`);
})
.listen(httpListenerConfig.port, () => {
console.log('listening..');
});
}
and I have to write unit tests for func1 and func2 (these functions are private),I want to invoke them using fake http request..
any idea?
You can use framework like superTest to test the http request. SuperTest needs the express app and so I am exporting the app.
I am assigning app.listen to server so that the server can be closed (server.close) after the test.
httpListener.js
var express = require('express');
function startListening() {
const app = express();
app
.get('/home/about', func1)
.get('/user/product/:id', func2)
.use(function (req, res) {
res.status(404).send(`no routing for path ${req.url}`);
})
var server = app.listen(3001, () => { //so the server can be closed after the test
console.log('listening..');
});
module.exports = server;
}
function func1 (req, res) {
res.status(200).send('this is home - about page');
}
function func2 (req, res) {
res.status(200).send('this is product page');
}
startListening();
httpListener-test.js
var request = require('supertest');
describe('loading express', function () {
var server;
beforeEach(function () {
server = require('./httpListner.js');
});
afterEach(function () {
server.close();
});
it('responds to /home/about', function test(done) {
request(server)
.get('/home/about')
.expect(200) //test status
.expect('this is home - about page', done); //test the response string
});
});
To test more on func1 and func2, you have to export them so it is available to test.