Skip to content

Commit

Permalink
fix(server): unregister error event listener (#9245)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Roe <daniel@roe.dev>
  • Loading branch information
matthieusieben and danielroe committed May 22, 2021
1 parent c56d484 commit e687842
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 10 additions & 2 deletions packages/server/src/listener.js
Expand Up @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions packages/server/test/listener.test.js
Expand Up @@ -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
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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
})
Expand Down

0 comments on commit e687842

Please sign in to comment.