diff --git a/package.json b/package.json index 979709e..59dfb81 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/server/server.js b/server/server.js index 2435d26..f4b05ab 100644 --- a/server/server.js +++ b/server/server.js @@ -23,4 +23,6 @@ app.post('/todos', (req, res) => { app.listen(3000, () => { console.log('Started on port: 3000'); -}); \ No newline at end of file +}); + +module.exports = { app }; \ No newline at end of file diff --git a/server/tests/server.test.js b/server/tests/server.test.js new file mode 100644 index 0000000..f74e005 --- /dev/null +++ b/server/tests/server.test.js @@ -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)); + }); + }); +}); \ No newline at end of file