I'm new to test driven development and am trying to use supertest to teach myself. I'm confused as to why I keep getting the error "app is not defined"? It is from my calls to request(app) which I have bolded below. I tried to look up the documentation but there doesn't seem to be much. All of my routes are in a file called "middleware", and that file starts like this:
var bodyParser = require('body-parser');
var helpers = require('./helpers.js'); // our custom middleware
var db = require('../DB/DB.js');
var router = require('../routes.js');
var path = require('path');
var fs = require('fs');
var gm = require('gm');
module.exports = function (app, express) {}
The actual file where I use supertest to test the middleware file's routes:
var chai = require('chai')
var assert = chai.assert;
var should = chai.should();
var expect = chai.expect;
var helpers = require("../config/helpers.js");
var middleware = require("../config/middleware.js");
// for when we eventually want to test against mock data
var fs = require('fs');
var path = require('path');
var supertest = require("supertest")(middleware);
describe('middleware API', function() {
it('responds with binary data', function(done) {
var imagePath = path.join(__dirname, '/../assets/drawings/', userName + '.png');
**request(app)**
.get(imagePath)
.expect(201)
.expect('Content-Type', 'image.png')
.parse(binaryParser)
.end(function(err, res) {
if (err) return done(err);
// binary response data is in res.body as a buffer
assert.ok(Buffer.isBuffer(res.body));
console.log("res=", res.body);
done();
});
});
it('sends back one image', function(done) {
**request(app)**
.get('/game/')
.expect(201)
.expect('Content-Type', 'image.png')
.expect('Content-Length', '1')
.parse(binaryParser)
.end(function(err, res) {
if (err) return done(err);
// binary response data is in res.body as a buffer
assert.ok(Buffer.isBuffer(res.body));
console.log("res=", res.body);
done();
})
})
})
In the top of your file you need to define express and app, ie:
var express = require('express'),
app = express();
Also, make sure that you've installed express, ie thru command line in node:
npm install express --save
(using "--save" adds it to your package.json file, which makes it easy to keep track of the version, etc, but also installs automatically if somebody else installs your project using npm install).