6

I'm using hapi, not express. Should supertest still work?

If so, is there a quick way to change my code to get it to run?

My test looks like this, based on the documentation:

import tape = require('tape');
const supertest = require('supertest');
const app = require('../../../../src/app');

tape('creates new user in database', function (assert) {
  supertest(app)
    .get('/ekapi/v1/signup')
    .expect(200)
    ......
});

but it gives me this error:

dist/server/app/node_modules/supertest/lib/test.js:55
  var addr = app.address();
                 ^

TypeError: app.address is not a function
    at Test.serverAddress (/home/rje/projects/ekaya/dist/server/app/node_modules/supertest/lib/test.js:55:18)

Here's my app code:

app.ts

import './core/externalTypes/imports';
import 'reflect-metadata';
import kernel from './core/inversify.config';
import {IServer}  from './core/types/IServer';

let server = kernel.get<IServer>("IServer");
server.start();

server.ts

import _ = require('lodash');
import * as hapi from "hapi";
import { injectable, inject } from "inversify";
import { IServer } from "../core/types/IServer";
import { ISignUpRoute } from "../core/types/ISignUpRoute";
import { IRouteHandler } from "../core/types/IRouteHandler";
import { IDatabase } from "../core/types/IDatabase";
import { IConfig } from "../core/types/IConfig";
import { IValidator } from "../core/types/IValidator";

@injectable()
class Server implements IServer
{
    _db : IDatabase;
    _server: hapi.Server;
    _config: IConfig;
    _validator: IValidator;
    _routes: [IRouteHandler];

   constructor(
        @inject("IDatabase") db: IDatabase,
        @inject("IConfig") config: IConfig,
        @inject("IValidator") validator: IValidator,
        @inject("ISignUpRoute") signUpRoute: ISignUpRoute)
    {
        this._db = db;
        this._config = config;
        this._validator = validator;
        this._routes = [signUpRoute];
      this._server = new hapi.Server();
      this._server.connection({
          host: '0.0.0.0',
          port: this._config.webservice_port,
             routes: { cors: true }
      });
        this.addRoutes();
   }

    start() : void
    {
        this._server.start((err : any) => {
             if (err)
                  throw err;
             console.log('Hapi server running at: ', this._server.info.uri);
        });
    }

    addRoutes() : void {
        const self = this;
        this._routes.map(function(routeHandler : IRouteHandler) {
            self._server.route(routeHandler.getRoute());
        });
    }

}

export = Server;
simon-p-r
  • 3,623
  • 2
  • 20
  • 35
Richard
  • 14,798
  • 21
  • 70
  • 103
  • 1
    superagent deals with Node HTTP server. Try to supply [`server.listener`](http://hapijs.com/api#serverlistener) to it, this may work. – Estus Flask May 25 '16 at 14:46
  • But if I have to start the server manually to get server.listener to pass to supertest, surely I may as well then just start it manually and query it using https://github.com/request/request. There's no benefit to supertest in this case. – Richard May 25 '16 at 19:55
  • Your code doesn't mention what `app.ts` exports. If it would export server instance, you could possibly do `supertest(app.listener)`. – Estus Flask May 25 '16 at 20:17
  • It doesn't export anything. I run app.js, and it starts the server. app.js is the root of my application. But I could add an export. Like app.server.listener or something, thanks. – Richard May 25 '16 at 20:34

2 Answers2

15

Minimal code sample for testing written in js:

'use strict';

const Hapi = require('hapi');
const should = require('chai').expect
const request = require('supertest')


const server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    reply('Hello, world!');
  }
});

describe('server', function() {
  it('server', function(done) {
    request(server.listener).get('/').expect(200, function(err, resp) {
      should(resp.text).equal('Hello, world!')
      done()
    })
  })
})

In case of "hapi": "^16" works as is.

In case of "hapi": "^8.1.0" you need to manually run server.start() berfore request

kharandziuk
  • 12,020
  • 17
  • 63
  • 121
1

Estus' answer worked. I needed to export the hapi.Server in my outermost class, app.js. Then pass its listener property to tape.

And then I had to use tape.onFinish( () => server.stop() ); to stop the server at the end of my tests.

Richard
  • 14,798
  • 21
  • 70
  • 103
  • Update: for anyone else having trouble with supertest I gave up on it and just used superagent directly, much easier. supertest seems abandoned, very few maintenance fixes lately. – Richard May 26 '16 at 14:14