I am currently delving into the world of test driven development and am taking on the challenge of using supertest to enhance my skills. I am facing an issue where I keep encountering the error message "app is not defined" when making calls to request(app), as highlighted in my code snippet below. I have attempted to consult the documentation for guidance, but unfortunately, it appears to be lacking. My routes are all contained within a file named "middleware", which begins as shown:
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) {}
In the actual file where I utilize supertest to test the routes within the middleware file:
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();
})
})
})