mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.61k stars 3.02k forks source link

Node 12 process.removeListener is not a function #3895

Closed whyboris closed 4 years ago

whyboris commented 5 years ago

Prerequisites

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

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

plroebuck commented 5 years ago

Node documentation link should be to this.

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

whyboris commented 5 years ago

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 commented 5 years ago

@whyboris is this still a pending issue?

whyboris commented 5 years ago

Yes: image

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

juergba commented 5 years ago

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

whyboris commented 5 years ago

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

mikeebowen commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

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.

rgroothuijsen commented 4 years ago

@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 commented 4 years ago

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

whyboris commented 4 years ago

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 commented 4 years ago

@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.

john-perks commented 3 years ago

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 commented 3 years ago

@john-perks thank you.

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