Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node 12 process.removeListener is not a function #3895

Closed
4 tasks done
whyboris opened this issue Apr 29, 2019 · 17 comments
Closed
4 tasks done

Node 12 process.removeListener is not a function #3895

whyboris opened this issue Apr 29, 2019 · 17 comments
Labels
area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer

Comments

@whyboris
Copy link

whyboris commented Apr 29, 2019

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Running unit tests with "mocha": "6.1.4", on my repository along with Node v12.0.0 results in error process.removeListener is not a function

This error does not occur with Node v10.15.1

Steps to Reproduce

I suspect the error will occur any time you run unit tests with mocha along with Node 12, but here's a way:

  1. Clone my repository:
    git clone https://github.com/whyboris/karma-helpful-reporter.git

  2. Install Node v12.0.0 (I'm using nvm to switch back between versions easily)

  3. Then npm run test and see the error.
    Note that the error does not occur on Node v10.

Expected behavior: [What you expect to happen]
No error with Node 12

Actual behavior: [What actually happens]

    process.removeListener('uncaughtException', uncaught);
            ^

TypeError: process.removeListener is not a function

Reproduces how often: [What percentage of the time does it reproduce?]
Every time

Versions

"mocha": "6.1.4",
Node v12.0.0

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.1.4
  • The output of node --version: v12.0.0
  • Your operating system
    • name and version: Mac OS 10.14.6
    • architecture (32 or 64-bit): 64bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): zsh
  • Your browser and version (if running browser tests): n/a
  • Any third-party Mocha-related modules (and their versions): n/a (?)
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): TypeScript (?)

Additional Information

Full log:

  1) "after each" hook
/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:917
    process.removeListener('uncaughtException', uncaught);
            ^

TypeError: process.removeListener is not a function
    at Runner.<anonymous> (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:917:13)
    at Runner.emit (events.js:196:13)
    at /Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:903:12
    at done (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:728:16)
    at done (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at Runner.hookErr (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:575:7)
    at Runner.uncaught (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:857:19)
    at process.uncaught (/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:887:10)
    at process.emit (events.js:196:13)
    at process._fatalException (internal/process/execution.js:146:25)

Node documentation for removeListener:
https://nodejs.org/api/events.html#events_event_removelistener
[edit] - documentation should be here, thank you @plroebuck :
https://nodejs.org/api/events.html#events_emitter_removelistener_eventname_listener

@whyboris whyboris changed the title Node 12 process.removeListener Node 12 process.removeListener is not a function Apr 29, 2019
@plroebuck
Copy link
Contributor

Node documentation link should be to this.

Does this occur with the built-in reporters, or just your custom one?

@whyboris
Copy link
Author

whyboris commented Apr 30, 2019

Thank you @plroebuck for the documentation link and the follow up question.

I suspect the problem will happen with any project that uses Mocha and attempts to run it with Node 12. My reporter does not have any dependencies, just devDependencies:
https://github.com/whyboris/karma-helpful-reporter/blob/karma-helpful-reporter/package.json#L13

The error occurs not when running my reporter in another project, but trying to run unit tests I've written using Mocha. The error occurs after all 105 of my unit test successfully run and pass.

🤷‍♂ sorry I'm not very knowledgeable about all this 😅

@juergba
Copy link
Contributor

juergba commented Aug 28, 2019

@whyboris is this still a pending issue?

@whyboris
Copy link
Author

whyboris commented Aug 28, 2019

Yes:
image

I even tried updating my repository to use "mocha": "6.2.0" 🤷‍♂

@juergba
Copy link
Contributor

juergba commented Aug 29, 2019

Have you updated your Node version? Which version do you use now?

@whyboris
Copy link
Author

I'm on Node 12.6.0 and the above screenshot is what I see.

@mikeebowen
Copy link

I'm also seeing exactly the same issue with mocha tests in node 12.12.0 and the screenshot above is what I also see.

@Munter
Copy link
Contributor

Munter commented Nov 4, 2019

I just tried to recreate this without success:

$ mocha --version
6.2.2
$ node -v
v12.13.0

Mocha's unit tests are also all run on node 12: https://travis-ci.org/mochajs/mocha/builds/606685504

It doesn't seem to be a problem with the base package, or any of the built-in reporters that are currently being tested.

@Munter
Copy link
Contributor

Munter commented Nov 4, 2019

I can recreate the error by cloning git@github.com:whyboris/karma-helpful-reporter, creating a clean install on node 12.13.0 and running the test suite:

$ npm t

> karma-helpful-reporter@0.3.4 test /Users/pbm/git/karma-helpful-reporter
> nyc mocha



[REMOVED SUCCESSFUL TESTS OUTPUT]


  105 passing (1s)

  1) "after each" hook
/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:917
    process.removeListener('uncaughtException', uncaught);
            ^

TypeError: process.removeListener is not a function
    at Runner.<anonymous> (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:917:13)
    at Runner.emit (events.js:210:5)
    at /Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:903:12
    at done (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:728:16)
    at done (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at done (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:761:7)
    at next (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:732:14)
    at Runner.hookErr (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:575:7)
    at Runner.uncaught (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:857:19)
    at process.uncaught (/Users/pbm/git/karma-helpful-reporter/node_modules/mocha/lib/runner.js:887:10)
    at process.emit (events.js:210:5)
    at processEmit [as emit] (/Users/pbm/git/karma-helpful-reporter/node_modules/signal-exit/index.js:155:32)
    at process._fatalException (internal/process/execution.js:150:25)

@Munter Munter added type: bug a defect, confirmed by a maintainer area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" and removed unconfirmed-bug labels Nov 4, 2019
@Munter
Copy link
Contributor

Munter commented Nov 4, 2019

Since we test on node 12 it seems that this code path must be untested. First order of business would be to recreate the bug in a test in a Pull-request we can the iterate on to fix it

@juergba
Copy link
Contributor

juergba commented Nov 18, 2019

I haven't understood what happens exactly. I just have some remarks:

  • process is a global object created by Node and is an instance of EventEmitter. Every EventEmitter should know the removeListener function. So the error TypeError: process.removeListener is not a function is very strange.
  • I suppose the process object is overridden somewhere.
  • the test file "data.types.test.js" is causing the error. I did not study the entire file, but I can see, process is manipulated.

I'm not very experienced in mocking, but I'm shyly passing the ball back to you.
My diagnosis so far: process has been mocked and not reverted to its original state.

@juergba juergba added the status: waiting for author waiting on response from OP - more information needed label Dec 12, 2019
@rgroothuijsen
Copy link
Contributor

@juergba Correct, the tests are stubbing process here and here. Since it's a global object, this altered state is carried over into other test files. If I comment out these lines, process works as expected.

@juergba
Copy link
Contributor

juergba commented Mar 24, 2020

@rgroothuijsen thank you for your confirmation. 👍
@whyboris can I close this issue?

@whyboris
Copy link
Author

whyboris commented Mar 24, 2020

I had this issue with version 6.1.4 -- my tests would generate this error:

  1) "after each" hook
/Users/byakubchik/Desktop/temp/karma-helpful-reporter/node_modules/mocha/lib/runner.js:917
    process.removeListener('uncaughtException', uncaught);
            ^

I updated to 7.1.1 -- now the error is just

1) "after each" hook in "util/shell.js test suite"

It's not clear what the error is, but at least it points to a file in my repository, rather than to mocha

Please feel free to close the issue if you think you've resolved it 👌
I suspect it is fixed, but can't be sure 🤷‍♂
Cheers 🙂

@juergba
Copy link
Contributor

juergba commented Mar 25, 2020

@whyboris I'm closing this issue.
I think it's your turn now to improve your testing code. Mocha needs the uncaught error handler to process uncaught exception. Overwriting the process object is a severe error in your code. It's your turn.

@juergba juergba added unconfirmed-bug and removed status: waiting for author waiting on response from OP - more information needed labels Mar 25, 2020
@juergba juergba closed this as completed Mar 25, 2020
@john-perks
Copy link

I recently encountered this error when upgrading Node through v12, and in case anyone else is having trouble with it...

It may relate to the change in v12 of making global.process non-enumerable, so some mocking/injection frameworks when asked to stub process in a module accidentally overwrite the real thing. This will be missing any method not needed for the test, hence the error.

In my case the framework was rewire, and the underlying bug can be found here, where I have suggested a workaround.

If that doesn't apply in your case, then the following may at least ease the symptoms if not address the root cause:

// global-process-hook.js
const oldProcess = process;

afterEach(() => {
    if (process !== oldProcess) {
        console.error('GLOBAL PROCESS CLOBBERED');
        process = oldProcess;
    }
});

Adding global-process-hook.js to the mocha command-line before the actual test folders allowed me to narrow down the tests that were causing the problem.

@juergba
Copy link
Contributor

juergba commented Aug 25, 2021

@john-perks thank you.

This issue and its reproduction repository are requiring rewire as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

7 participants