-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Comments
Hi @thomrick, 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;
}); |
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:
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. I didn't try yet to execute more than one test file but I smell a problem with the listen()... |
Hi @thomrick, |
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. |
Hey !
I don't know how to test Nest api routes with supertest.
Someone already try this ?
The text was updated successfully, but these errors were encountered: