Trying to get a better understanding of it, I am testing this simple Express server (v 4 ) setup code :
server.js
import express from 'express';
import router from './routes';
const app = express();
app.use(router);
const server = app.listen(3000, function () {
var port = this.address().port;
/* eslint-disable no-console */
console.log('Example app listening on port %s!', port);
});
export default app; // as suggested by Ron
with the following router
routes.js
var express = require('express');
var defaultController = require('./controllers/defaultController');
var router = express.Router();
router.get('/', defaultController.getHome);
module.exports = router;
it's clean ( eslinted) and running well with yarn start
I wrote ( copy/paste from tuts .. written in 2015) the following spec to test server.js with Jest and SuperTest
server.spec.js
var request = require('supertest');
describe('loading express', function () {
var server; // http server object
beforeEach(function () {
delete require.cache[require.resolve('../src/server')];
server = require('../src/server');
});
afterEach(function () {
server.close();
});
it('responds to /', function testSlash(done) {
request(server)
.get('/')
.expect(200, done);
});
it('404 everything else', function testPath(done) {
request(server)
.get('/foo/bar')
.expect(404, done);
});
});
But it's failing... with type error on .get()
.
TypeError: app.address is not a function
and type error on server.clos()
TypeError: server.close is not a function
Here is the full console.log
$ yarn test
yarn run v1.9.4
$ jest --runInBand --verbose
FAIL test/server.spec.js
loading express
✕ responds to / (275ms)
✕ 404 everything else (1ms)
● loading express › responds to /
TypeError: app.address is not a function
16 | it('responds to /', function testSlash(done) {
17 | request(server)
> 18 | .get('/')
| ^
19 | .expect(200, done);
20 | });
21 |
at Test.Object.<anonymous>.Test.serverAddress (node_modules/supertest/lib/test.js:55:18)
at new Test (node_modules/supertest/lib/test.js:36:12)
at Object.obj.(anonymous function) [as get] (node_modules/supertest/index.js:25:14)
at Object.get (test/server.spec.js:18:12)
● loading express › responds to /
TypeError: server.close is not a function
11 |
12 | afterEach(function () {
> 13 | server.close();
| ^
14 | });
15 |
16 | it('responds to /', function testSlash(done) {
at Object.close (test/server.spec.js:13:16)
● loading express › 404 everything else
TypeError: app.address is not a function
22 | it('404 everything else', function testPath(done) {
23 | request(server)
> 24 | .get('/foo/bar')
| ^
25 | .expect(404, done);
26 | });
27 | });
at Test.Object.<anonymous>.Test.serverAddress (node_modules/supertest/lib/test.js:55:18)
at new Test (node_modules/supertest/lib/test.js:36:12)
at Object.obj.(anonymous function) [as get] (node_modules/supertest/index.js:25:14)
at Object.get (test/server.spec.js:24:12)
● loading express › 404 everything else
TypeError: server.close is not a function
11 |
12 | afterEach(function () {
> 13 | server.close();
| ^
14 | });
15 |
16 | it('responds to /', function testSlash(done) {
at Object.close (test/server.spec.js:13:16)
console.log src/server.js:11
Example app listening on port 3000!
Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 1.062s
Ran all test suites.
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.