Files
nest/integration/nest-application/get-url/e2e/fastify.spec.ts
Jay McDoniel f36cb0c729 fix(application): makes get-url work in callback
`app.getUrl()` was throwing an error when `this.isListening`
was set to false. The value was set _after_ `this.httpServer.listen`
was called, now it is set _before_ to ensure that the `app.getUrl()`
can be called in the callback of `listen`. A regression test has
been added for this, and all tests related to the change are passing.

fix: #5798
2020-11-26 14:18:39 -08:00

51 lines
1.6 KiB
TypeScript

import { FastifyAdapter } from '@nestjs/platform-fastify';
import { Test, TestingModule } from '@nestjs/testing';
import { expect } from 'chai';
import { AppModule } from '../src/app.module';
import { randomPort } from './utils';
describe('Get URL (Fastify Application)', () => {
let testModule: TestingModule;
let port: number;
beforeEach(async () => {
testModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
});
beforeEach(async () => {
port = await randomPort();
});
it('should be able to get the IPv4 address', async () => {
const app = testModule.createNestApplication(new FastifyAdapter());
await app.listen(port, '127.0.0.5');
expect(await app.getUrl()).to.be.eql(`http://127.0.0.5:${port}`);
await app.close();
});
it('should return 127.0.0.1 for 0.0.0.0', async () => {
const app = testModule.createNestApplication(new FastifyAdapter());
await app.listen(port, '0.0.0.0');
expect(await app.getUrl()).to.be.eql(`http://127.0.0.1:${port}`);
await app.close();
});
it('should return 127.0.0.1 even in a callback', () => {
const app = testModule.createNestApplication(new FastifyAdapter());
return app.listen(port, async () => {
expect(await app.getUrl()).to.be.eql(`http://127.0.0.1:${port}`);
await app.close();
});
});
it('should throw an error for calling getUrl before listen', async () => {
const app = testModule.createNestApplication(new FastifyAdapter());
try {
await app.getUrl();
} catch (err) {
expect(err).to.be.eql(
'app.listen() needs to be called before calling app.getUrl()',
);
}
});
});