Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:08:42+00:00 2026-05-29T07:08:42+00:00

I would like to test that the following function performs as expected: function throwNextTick(error)

  • 0

I would like to test that the following function performs as expected:

function throwNextTick(error) {
    process.nextTick(function () {
        throw error;
    });
}

Here is my attempt:

describe("throwNextTick", function () {
    it("works as expected", function (next) {
        var error = new Error("boo!");
        var recordedError = null;
        process.once("uncaughtException", function (error) {
            recordedError = error;
        });

        throwNextTick(error);

        process.nextTick(function () {
            recordedError.should.be(error);
            next();
        });
    });
});

But mocha seems to want to keep any errors to itself, and fail my test when it gets them:

C:\Users\ddenicola\Programming (Synced)\pubit>mocha test/basicTest.js

  throwNextTick
    0) works as expected

  ? 1 of 1 tests failed:

  1) throwNextTick works as expected:
     Error: boo!
      at Test.fn (C:\Users\ddenicola\Programming (Synced)\pubit\test\basicTest.js:11:21)
      at Test.run (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:144:15)
      at Runner.runTest (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:271:10)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:315:12
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:199:14)
      at C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:208:7
      at next (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:157:23)
      at Array.0 (C:\Users\ddenicola\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:176:5)
      at EventEmitter._tickCallback (node.js:192:40)

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:08:43+00:00Added an answer on May 29, 2026 at 7:08 am

    Update: Courtesy of casey-foster in a comment below:

    As of node v6.0.0 you can use process.prependOnceListener('uncaughtException', ...) to do this much more succinctly.


    Old answer:

    The secret lies in process.listeners(‘uncaughtException’):

    http://nodejs.org/docs/latest/api/events.html#emitter.listeners

    Simply remove the mocha listener, add your own, then reattach the mocha listener.

    See below:

    var assert = require('assert')
    
    function throwNextTick(error) {
        process.nextTick(function () {
            throw error
        })
    }
    
    
    describe("throwNextTick", function () {
        it("works as expected", function (next) {
            var error = new Error("boo!")
            var recordedError = null
            var originalException = process.listeners('uncaughtException').pop()
            //Needed in node 0.10.5+
            process.removeListener('uncaughtException', originalException);
            process.once("uncaughtException", function (error) {
                recordedError = error
            })
            throwNextTick(error);
            process.nextTick(function () {
                process.listeners('uncaughtException').push(originalException)
                assert.equal(recordedError, error)
                next()
            })
        })
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to test the following helper module function: module UploadsHelper def custom_img_tag(upload,
I would like to test an app that uses the Clipboard (WindowsForms) and I
I have 2 actions in a Flash file that I would like to test
What would a simple unit test look like to confirm that a certain controller
I would like to test a function with a tuple from a set of
Given the following method: public function foo($callback) { call_user_func($callback); } How would I test
I would like to put some logging statements within test function to examine some
i have the following module and i would like to test connection. how do
I would like to convert a string that I pass in a function into
I would like to test a string containing a path to a file for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.