Repository: nuxt/nuxt.js
Pull request #9245 (2021-05-06 12:22:28)

fix(server): unregister error event listener
<!--- Provide a general summary of your changes in the title above -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [x] Bug fix (a non-breaking change which fixes an issue)
- [ ] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)


## Description
<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it resolves an open issue, please link to the issue here. For example "Resolves: #1337" -->


## Checklist:
<!--- Put an `x` in all the boxes that apply. -->
<!--- If your change requires a documentation PR, please link it appropriately -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly. (PR: #)
- [ ] I have added tests to cover my changes (if not applicable, please state why)
- [x] All new and existing tests are passing.



codecov-commenter: # [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt) Report
> Merging [#9245](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt) (3cecf38) into [dev](https://codecov.io/gh/nuxt/nuxt.js/commit/c56d4840ad4d67d6883f1a7317b965c2f8073ac7?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt) (c56d484) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 3cecf38 differs from pull request most recent head f4a39c6. Consider uploading reports for the commit f4a39c6 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/nuxt/nuxt.js/pull/9245/graphs/tree.svg?width=650&height=150&src=pr&token=nGD1PtMB3M&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt)](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt)

```diff
@@            Coverage Diff             @@
##              dev    #9245      +/-   ##
==========================================
+ Coverage   65.13%   65.16%   +0.03%     
==========================================
  Files          94       94              
  Lines        4101     4105       +4     
  Branches     1124     1124              
==========================================
+ Hits         2671     2675       +4     
  Misses       1152     1152              
  Partials      278      278              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `65.16% <100.00%> (+0.03%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt) | Coverage Δ | |
|---|---|---|
| [packages/server/src/listener.js](https://codecov.io/gh/nuxt/nuxt.js/pull/9245/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt#diff-cGFja2FnZXMvc2VydmVyL3NyYy9saXN0ZW5lci5qcw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt). Last update [c56d484...f4a39c6](https://codecov.io/gh/nuxt/nuxt.js/pull/9245?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nuxt).


Patch:
diff --git a/packages/server/src/listener.js b/packages/server/src/listener.js
index b9491a43b..8f9a8ca45 100644
--- a/packages/server/src/listener.js
+++ b/packages/server/src/listener.js
@@ -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)


Test patch:
diff --git a/packages/server/test/listener.test.js b/packages/server/test/listener.test.js
index b6c01c78c..fa4365e50 100644
--- a/packages/server/test/listener.test.js
+++ b/packages/server/test/listener.test.js
@@ -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
     })