-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): change listen to reject on server bind failures
pr update
- Loading branch information
1 parent
9e20b0a
commit 4cc2d18
Showing
14 changed files
with
230 additions
and
34,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ExpressAdapter } from '@nestjs/platform-express'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import * as express from 'express'; | ||
import { AppModule } from '../src/app.module'; | ||
import { INestApplication } from '@nestjs/common'; | ||
|
||
describe('Listen (Express Application)', () => { | ||
let testModule: TestingModule; | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
testModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
app = testModule.createNestApplication(new ExpressAdapter(express())); | ||
}); | ||
|
||
afterEach(async () => { | ||
app.close(); | ||
}); | ||
|
||
it('should resolve with httpServer on success', async () => { | ||
const response = await app.listen(3000); | ||
expect(response).to.eql(app.getHttpServer()); | ||
}); | ||
|
||
it('should reject if the port is not available', async () => { | ||
await app.listen(3000); | ||
const secondApp = testModule.createNestApplication( | ||
new ExpressAdapter(express()), | ||
); | ||
try { | ||
await secondApp.listen(3000); | ||
} catch (error) { | ||
expect(error.code).to.equal('EADDRINUSE'); | ||
} | ||
}); | ||
|
||
it('should reject if there is an invalid host', async () => { | ||
try { | ||
await app.listen(3000, '1'); | ||
} catch (error) { | ||
expect(error.code).to.equal('EADDRNOTAVAIL'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { FastifyAdapter } from '@nestjs/platform-fastify'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import { AppModule } from '../src/app.module'; | ||
import { INestApplication } from '@nestjs/common'; | ||
|
||
describe('Listen (Fastify Application)', () => { | ||
let testModule: TestingModule; | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
testModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
app = testModule.createNestApplication(new FastifyAdapter()); | ||
}); | ||
|
||
afterEach(async () => { | ||
app.close(); | ||
}); | ||
|
||
it('should resolve with httpServer on success', async () => { | ||
const response = await app.listen(3000); | ||
expect(response).to.eql(app.getHttpServer()); | ||
}); | ||
|
||
it('should reject if the port is not available', async () => { | ||
await app.listen(3000); | ||
const secondApp = testModule.createNestApplication(new FastifyAdapter()); | ||
try { | ||
await secondApp.listen(3000); | ||
} catch (error) { | ||
expect(error.code).to.equal('EADDRINUSE'); | ||
} | ||
|
||
await secondApp.close(); | ||
}); | ||
|
||
it('should reject if there is an invalid host', async () => { | ||
try { | ||
await app.listen(3000, '1'); | ||
} catch (error) { | ||
expect(error.code).to.equal('EADDRNOTAVAIL'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.sayHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
sayHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.