Skip to content

Commit

Permalink
Adds tests for POST /todos
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlockDruid committed Sep 24, 2018
1 parent 9482495 commit 8b22a0d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"expect": "^23.6.0",
"express": "^4.16.3",
"mocha": "^5.2.0",
"mongodb": "^3.1.6",
"mongoose": "^5.2.17"
"mongoose": "^5.2.17",
"nodemon": "^1.18.4",
"supertest": "^3.3.0"
}
}
4 changes: 3 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ app.post('/todos', (req, res) => {

app.listen(3000, () => {
console.log('Started on port: 3000');
});
});

module.exports = { app };
34 changes: 34 additions & 0 deletions server/tests/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const expect = require('expect');
const request = require('supertest');

const { app } = require('./../server');
const { Todo } = require('./../models/todo');

beforeEach((done) => {
Todo.deleteMany().then(() => done());
});

describe('POST /todos', () => {
it('should create a new todo', (done) => {
let text = "Test to todo text";

request(app)
.post('/todos')
.send({text})
.expect(200)
.expect((res) => {
expect(res.body.text).toBe(text);
})
.end((err, res) => {
if (err) {
return done(err);
}

Todo.find().then((todos) => {
expect(todos.length).toBe(1);
expect(todos[0].text).toBe(text);
done();
}).catch((e) => done(e));
});
});
});

0 comments on commit 8b22a0d

Please sign in to comment.