fix(server): unregister error event listener (#9245)

Co-authored-by: Daniel Roe <daniel@roe.dev>
This commit is contained in:
Matthieu Sieben 2021-05-22 13:42:02 +02:00 committed by GitHub
parent c56d4840ad
commit e687842536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 10 deletions

View File

@ -75,8 +75,16 @@ export default class Listener {
// Call server.listen
try {
this.server = await new Promise((resolve, reject) => {
this._server.on('error', error => reject(error))
const s = this._server.listen(listenArgs, error => error ? reject(error) : resolve(s))
this._server.once('error', reject)
this._server.listen(listenArgs, (error) => {
this._server.off('error', reject)
if (error) {
reject(error)
} else {
resolve(this._server)
}
})
})
} catch (error) {
return this.serverErrorHandler(error)

View File

@ -17,7 +17,8 @@ describe('server: listener', () => {
const mockServer = () => {
const server = {
address: jest.fn(),
on: jest.fn(),
once: jest.fn(),
off: jest.fn(),
listen: jest.fn((listenArgs, callback) => {
Promise.resolve().then(callback)
return server
@ -73,8 +74,8 @@ describe('server: listener', () => {
expect(http.createServer).toBeCalledTimes(1)
expect(http.createServer).toBeCalledWith(options.app)
expect(server.on).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function))
expect(server.once).toBeCalledTimes(1)
expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith(
{
@ -111,8 +112,8 @@ describe('server: listener', () => {
expect(https.createServer).toBeCalledTimes(1)
expect(https.createServer).toBeCalledWith(options.https, options.app)
expect(server.on).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function))
expect(server.once).toBeCalledTimes(1)
expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith(
{
@ -150,8 +151,8 @@ describe('server: listener', () => {
expect(http.createServer).toBeCalledTimes(1)
expect(http.createServer).toBeCalledWith(options.app)
expect(server.on).toBeCalledTimes(1)
expect(server.on).toBeCalledWith('error', expect.any(Function))
expect(server.once).toBeCalledTimes(1)
expect(server.once).toBeCalledWith('error', expect.any(Function))
expect(server.listen).toBeCalledTimes(1)
expect(server.listen).toBeCalledWith(
{
@ -204,7 +205,7 @@ describe('server: listener', () => {
const serverError = new Error('error occurred')
server.listen.mockImplementationOnce((listenArgs, callback) => {
Promise.resolve().then(callback)
const errorListener = server.on.mock.calls[0][1]
const errorListener = server.once.mock.calls[0][1]
errorListener(serverError)
return server
})