From b393814eee41c9733cdb451c80ea08576f9d99af Mon Sep 17 00:00:00 2001 From: Jeremy Morrell Date: Wed, 26 Jul 2017 12:55:16 -0700 Subject: [PATCH] Add integration tests for CI (#78) Add integration tests for Heroku CI --- index.js | 2 -- package.json | 7 ++++++- test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test.js diff --git a/index.js b/index.js index ea0ab74..96235c2 100644 --- a/index.js +++ b/index.js @@ -16,5 +16,3 @@ app.get('/', function(request, response) { app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); }); - - diff --git a/package.json b/package.json index 21243c2..b79cf67 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,17 @@ }, "main": "index.js", "scripts": { - "start": "node index.js" + "start": "node index.js", + "test": "node test.js" }, "dependencies": { "ejs": "2.5.6", "express": "4.15.2" }, + "devDependencies": { + "request": "^2.81.0", + "tape": "^4.7.0" + }, "repository": { "type": "git", "url": "https://github.com/heroku/node-js-getting-started" diff --git a/test.js b/test.js new file mode 100644 index 0000000..797c124 --- /dev/null +++ b/test.js @@ -0,0 +1,28 @@ +const { spawn } = require('child_process'); +const request = require('request'); +const test = require('tape'); + +// Start the app +const env = Object.assign({}, process.env, {PORT: 5000}); +const child = spawn('node', ['index.js'], {env}); + +test('responds to requests', (t) => { + t.plan(4); + + // Wait until the server is ready + child.stdout.on('data', _ => { + // Make a request to our app + request('http://127.0.0.1:5000', (error, response, body) => { + // stop the server + child.kill(); + + // No error + t.false(error); + // Successful response + t.equal(response.statusCode, 200); + // Assert content checks + t.notEqual(body.indexOf("Node.js Getting Started on Heroku"), -1); + t.notEqual(body.indexOf("Getting Started with Node on Heroku"), -1); + }); + }); +});