From a356286f9229788054b67c0f5a50520af0299096 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Oct 2020 09:27:41 -0700 Subject: [PATCH 1/4] add test setup --- README.md | 4 ++++ api/server.js | 4 ++++ index.js | 2 ++ package.json | 10 +++++++--- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ffcab92c..89b00339 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,7 @@ Guided project for **Node Server Testing** Module. - [ ] type `npm run server` to start the API. Please follow along as the instructor adds automated tests to the API. + +## Testing an API + +- run the server on a port diff --git a/api/server.js b/api/server.js index 52c80536..27619496 100644 --- a/api/server.js +++ b/api/server.js @@ -10,6 +10,10 @@ server.get("/", (req, res) => { res.status(200).json({ api: "up" }); }); +// returns http 200 +// returns json +// the body has an api property and the values is up + server.get("/hobbits", (req, res) => { Hobbits.getAll() .then(hobbits => { diff --git a/index.js b/index.js index c01aa681..253ac6d4 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,7 @@ require('dotenv').config(); const server = require('./api/server.js'); + + const port = process.env.PORT || 5000; server.listen(port, () => console.log(`\n** server up on port ${port} **\n`)); diff --git a/package.json b/package.json index 0d9f56bd..6c79e9bb 100644 --- a/package.json +++ b/package.json @@ -5,17 +5,21 @@ "main": "index.js", "scripts": { "server": "nodemon index.js", - "start": "node index.js" + "start": "node index.js", + "test": "jest --watch" }, "keywords": [], "author": "Lambda School", "dependencies": { "dotenv": "^8.2.0", "express": "^4.17.1", + "jest": "^26.5.3", "knex": "^0.21.6", - "sqlite3": "^5.0.0" + "sqlite3": "^5.0.0", + "supertest": "^5.0.0" }, "devDependencies": { "nodemon": "^2.0.5" - } + }, + "jest":{ "testEnvironment":"node"} } From 202909751c621581969ff29c8e6fe030adfd801b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Oct 2020 09:54:16 -0700 Subject: [PATCH 2/4] deploying --- README.md | 8 ++++++++ api/server.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 api/server.spec.js diff --git a/README.md b/README.md index 89b00339..3de868c2 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,11 @@ Please follow along as the instructor adds automated tests to the API. ## Testing an API - run the server on a port +- make an request to the endpoint (may or may not include data) +- inspect the result to the endpoint to see if it is what I expected + +- npm i supertest jest +- add test script : "test": "jest --watch" +- add environment : "jest":{ "testEnvironment":"node"} + +Jest will default to running code in an environment similar to a web browser. For testing node servers, we need to change that option. diff --git a/api/server.spec.js b/api/server.spec.js new file mode 100644 index 00000000..ad706c79 --- /dev/null +++ b/api/server.spec.js @@ -0,0 +1,22 @@ +const supertest = require('supertest') +const { intersect } = require('../data/dbConfig') +const server = require('./server') + + +describe('server', () => { + describe('GET', () => { + it ("should return 200", () => { + return supertest(server).get('/').then (res=> {expect(res.status).toBe(200)}) + }) + // it('should have a body', () => { + // return supertest(server).get('/').then(res => {expect(res.body).toEqual({api:"up"})} ) + // }) + it('should have a body api:up', () => { + return supertest(server).get('/').then(res => {expect(res.body.api).toBe("up")} ) + }) + it('should have a body api:up', () => { + return supertest(server).get('/').then(res => {expect(res.type).toMatch("up")} ) + }) + + }) +}) \ No newline at end of file From 793a6e6e8e0b8ba710c36afc964e93d7677b4cb7 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Oct 2020 09:56:01 -0700 Subject: [PATCH 3/4] deploying --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3de868c2..2fb881c9 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,4 @@ Please follow along as the instructor adds automated tests to the API. - add test script : "test": "jest --watch" - add environment : "jest":{ "testEnvironment":"node"} -Jest will default to running code in an environment similar to a web browser. For testing node servers, we need to change that option. +Jest will default to running code in an environment similar to a web browser. For testing node servers, we need to change that option From 3332b42a9220fc6859a7557f5315e6d7a4cddf89 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Oct 2020 10:34:51 -0700 Subject: [PATCH 4/4] add pg --- api/server.spec.js | 11 ++++++++--- package.json | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/api/server.spec.js b/api/server.spec.js index ad706c79..0845aaba 100644 --- a/api/server.spec.js +++ b/api/server.spec.js @@ -14,9 +14,14 @@ describe('server', () => { it('should have a body api:up', () => { return supertest(server).get('/').then(res => {expect(res.body.api).toBe("up")} ) }) - it('should have a body api:up', () => { - return supertest(server).get('/').then(res => {expect(res.type).toMatch("up")} ) - }) + + it("should return JSON", () => { + return supertest(server) + .get("/") + .then(res => { + expect(res.type).toMatch(/json/i); + }); + }); }) }) \ No newline at end of file diff --git a/package.json b/package.json index 6c79e9bb..2bd157f4 100644 --- a/package.json +++ b/package.json @@ -15,11 +15,14 @@ "express": "^4.17.1", "jest": "^26.5.3", "knex": "^0.21.6", + "pg": "^8.4.1", "sqlite3": "^5.0.0", "supertest": "^5.0.0" }, "devDependencies": { "nodemon": "^2.0.5" }, - "jest":{ "testEnvironment":"node"} + "jest": { + "testEnvironment": "node" + } }