Implemented very basic (and poorly written) unit tests that test using the live database

This commit is contained in:
Gregory Campbell
2021-11-25 10:48:28 -05:00
parent 98308642aa
commit 4af4f90a72
4 changed files with 7767 additions and 4 deletions
+7685 -1
View File
File diff suppressed because it is too large Load Diff
+6 -2
View File
@@ -5,7 +5,7 @@
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha --exit"
},
"repository": {
"type": "git",
@@ -36,6 +36,10 @@
"optionator": "^0.9.1"
},
"devDependencies": {
"eslint": "^8.3.0"
"eslint": "^8.3.0",
"jest": "^27.3.1",
"mocha": "^9.1.3",
"superagent": "^6.1.0",
"supertest": "^6.1.6"
}
}
+2 -1
View File
@@ -31,6 +31,7 @@ app.route("/")
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('App is listening on port ' + listener.address().port)
console.log('App is listening on port ' + listener.address().port)
})
module.exports = app;
+74
View File
@@ -0,0 +1,74 @@
const request = require('supertest');
const server = require('../server');
describe('GET /msg', function () {
it('respond with json containing a list of all users', function (done) {
request(server)
.get('/msg')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});;
describe('GET /msg/:id', function () {
it('respond with json containing a single message', function (done) {
request(server)
.get('/msg/1')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
/**
* Testing get a message endpoint by giving a non-existing message
*/
describe('GET /msg/:id', function () {
it('respond with json message not found', function (done) {
request(server)
.get('/users/idisnonexisting')
//.set('Accept', 'application/json')
.expect(404) //expecting HTTP status code
//.expect('"Message not found"') // expecting content value
.end((err) => {
if (err) return done(err);
done();
});
});
});
/**
* Testing post message endpoint
*/
describe('POST /msg', function () {
it('respond with 201 created', function (done) {
request(server)
.post('/msg/1/test')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end((err) => {
if (err) return done(err);
done();
});
});
});
/**
* Testing post message endpoint
*/
describe('POST /msg', function () {
it('respond with 400 not created', function (done) {
request(server)
.post('/msg/test')
.set('Accept', 'application/json')
//.expect('Content-Type', /json/)
.expect(404)
//.expect('"Message not created"')
.end((err) => {
if (err) return done(err);
done();
});
});
});