Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test Nest with supertest ? #70

Closed
thomrick opened this issue May 18, 2017 · 4 comments
Closed

How to test Nest with supertest ? #70

thomrick opened this issue May 18, 2017 · 4 comments

Comments

@thomrick
Copy link
Contributor

thomrick commented May 18, 2017

Hey !
I don't know how to test Nest api routes with supertest.
Someone already try this ?

@kamilmysliwiec
Copy link
Member

Hi @thomrick,
The same way as with express. It is possible to pass custom express instance as a second argument of NestFactory.create method.

import * as request from 'supertest';
import * as express from 'express';
import { NestFactory } from '@nestjs/core';

const instance = express();

@Controller() 
class UsersController {
    @Get('user')
    getUser(req, res) {
        res.status(200).json([]);
    }
}

@Module({
    modules: [],
    controllers: [UsersController],
})
export class ApplicationModule {}

const app = NestFactory.create(ApplicationModule, instance);
app.listen(3000, () => console.log('Application is listening on port 3000.'));

///////////// Test
request(instance)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect(200)
  .end(function(err, res) {
    if (err) throw err;
  });

@thomrick
Copy link
Contributor Author

Hey @kamilmysliwiec,

The thing is I don't know why you have to call the listen() function ?

I wrote this test file that work fine:

import {NestFactory} from '@nestjs/core';
import {RegistrationModule} from './registration.module';
import * as request from 'supertest';
import {expect} from 'chai';
import {ExpressAdapter} from '@nestjs/core/adapters/express-adapter';
import {Application} from 'express';
import {INestApplication} from '@nestjs/common/interfaces/nest-application.interface';

describe('RegistrationModule', () => {
  let instance: Application;

  before(() => {
    instance = ExpressAdapter.create();
    const application: INestApplication = NestFactory.create(RegistrationModule, instance);
    application.listen(8080, () => console.log('Application listening at port 8080'));
  });

  it('should expose GET /api/registrations endpoint', done => {
    request(instance)
      .get('/api/registrations')
      .end((error, response) => {
        expect(response.status).to.not.be.equal(404);
        done();
      });
  });

  it('should expose POST /api/registrations endpoint', done => {
    request(instance)
      .post('/api/registrations')
      .end((error, response) => {
        expect(response.status).to.not.be.equal(404);
        done();
      });
  })
});

But I couldn't write the test init like this:

beforeEach(() => {
    instance = ExpressAdapter.create();
    const application: INestApplication = NestFactory.create(RegistrationModule, instance);
    application.listen(8080, () => console.log('Application listening at port 8080'));
  });

Because it throws this exception:

Uncaught Error: listen EADDRINUSE :::8080
      at Object.exports._errnoException (util.js:1050:11)
      at exports._exceptionWithHostPort (util.js:1073:20)
      at Server.setupListenHandle [as _listen2] (net.js:1259:14)
      at listenInCluster (net.js:1307:12)
      at Server.listen (net.js:1406:7)
      at Function.listen (node_modules/@nestjs/core/node_modules/express/lib/application.js:617:24)
      at NestApplication.listen (node_modules/@nestjs/core/nest-application.js:26:29)
      at exceptions_zone_1.ExceptionsZone.run (node_modules/@nestjs/core/nest-factory.js:47:35)
      at Function.run (node_modules/@nestjs/core/errors/exceptions-zone.js:8:13)
      at Proxy.args (node_modules/@nestjs/core/nest-factory.js:46:70)
      at Context.beforeEach (src/application/modules/registration/registration.module.spec.ts:15:17)

So I though to close the server and I had looking for what return listen() function ==> nothing... not as express that it return the http.Server.
So impossible to close the server...

I didn't try yet to execute more than one test file but I smell a problem with the listen()...

kamilmysliwiec pushed a commit that referenced this issue May 23, 2017
…onfiguration class, multiple servers, global route prefix (#70, #20, #40)
@kamilmysliwiec
Copy link
Member

Hi @thomrick,
Update your packages into 2.1.0 version. Now listen() method should returns http server. Futhermore, I added close() methods in INestApplication and INestMicroservice.

@lock
Copy link

lock bot commented Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants