I'm trying to setup tests on an Express server using Jest and Supertest. Here is my test and target code:
// routes/index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
// index.test.js
const request = require('supertest');
const express = require('express');
const app = require('express');
const router = require('../index').router;
test('GET /', (done) => {
request(router)
.get('http://localhost:4000')
// .set('Accept', 'application/json')
.expect(200)
.end((err, res) => {
if (err) throw err;
done();
})
});
Jest is giving me the following error:
expected 200 "OK", got 404 "Not Found"
at Test.Object.<anonymous>.Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
at Test.Object.<anonymous>.Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
at Test.Object.<anonymous>.Test.assert (node_modules/supertest/lib/test.js:173:18)
at Server.localAssert (node_modules/supertest/lib/test.js:131:12)
Also tried: Relative urls ('/'). Error: Can not read property "apply" of undefined
Importing the router object exported in routes/index.js like so:
const router = require('../index').router;
// TypeError: Cannot read property 'address' of undefined
// (while pointing at the .get() method)
I suspect the way I'm bringing in Router() may not be right but my direct attempt didn't succeed.
Anyone have an idea of the issue?