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

  • Home
  • SEARCH
  • 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 4057746
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:59:29+00:00 2026-05-20T14:59:29+00:00

I trying to bend my head around the async nature of node.js and nodeunit.

  • 0

I trying to bend my head around the async nature of node.js and nodeunit.

now nodeunit is supposed to help me out but it gives me headaches as well:

$ nodeunit test_logfile.js 

test_logfile.js
✖ testLogentry

Error: Expected 1 assertions, 0 ran
    at Object.done (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/types.js:119:25)
    at /home/mark/devel/PerfDriver/test/test_logfile.js:48:10
    at Object.runTest (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:54:9)
    at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:90:21
    at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:508:13
    at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:118:13
    at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:134:9
    at /home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:507:9
    at Object.concatSeries (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/deps/async.js:147:23)
    at Object.runSuite (/home/mark/.node_libraries/.npm/nodeunit/0.5.1/package/lib/core.js:79:11)


FAILURES: 1/1 assertions failed (50ms)

node.js:116
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: ECONNREFUSED, Connection refused
    at Socket._onConnect (net.js:576:18)
    at IOWatcher.onWritable [as callback] (net.js:165:12)

here is my testcase:

var nodeunit = require('../lib/nodeunit/lib/nodeunit.js')
var http = require('http');
var logfile = require('../src/logfile.js');

var testmsg = 'this is my first testmessage:-_,.:;öüäß?1234"@€';


exports.testLogentry = function(test) {
    test.expect(1); // make sure all the async expectations are fulfilled

    var options = {
        host: 'localhost',
        port: 8123,
        path: '/',
        method: 'POST',
        record_message: function(msg) {
            console.log('received ' + msg + '\n');
            test.equal(msg, testmsg);
            return;
        }
    };

    var logger = logfile.loggerFactory(options);

    logfile.write(testmsg, options);
    logger.stop();
    test.done();
};

I guess my question is if assertions work well in callbacks or if there are any restrictions that might cause the testcase to fail.
My assumption is that nodeunit is able to cope with the asynchronous programming style. So I expect “test.equal(msg, testmsg);” to work. If I run the content of the testcase in the node console then “record_message” is called.
I would post logfile.js as well but this is probably to much and I am looking for more general advice.

Thanks.

Mark

  • 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-20T14:59:30+00:00Added an answer on May 20, 2026 at 2:59 pm

    I am not exactly sure how this is handled here generally but meanwhile I have found the solution to my problem and I post it here in case someone will struggle with the same issue.

    Now I can tell that the problem has to do with the asynchronous character of node.js, http server and unit testing under these circumstances.

    Anyways here is my solution:

    var nodeunit = require('../lib/nodeunit/lib/nodeunit.js');
    var http = require('http');
    var logfile = require('../src/logfile');
    
    var testmsg = 'this is my first testmessage:-_,.:;öüäß?1234"@€';
    
    
    var testutil = function (config, envReady) {
        // simple testutil to provide http server environment for unit testing
    
        var logger = logfile.loggerFactory(config);
    
        // actually this is the tricky part which ensures that the server
        // is still available once request/ response are executed
        process.nextTick(function () {
            if (envReady && typeof envReady === 'function') {
                envReady(logger, logfile.write);
            }
        });
    };
    
    
    exports.test_logfile = function (test) {
    
        test.expect(4);
    
        var options = {
            host: 'localhost',
            port: 3000,
            path: '/simple',
            method: 'PUT',
            record_message: function(msg) {
                test.equal(msg, testmsg);
            }
        };
    
        testutil(options, function (logger, write) {
            var write_ready = function(res) {
                // called when write is completed
                test.equal(res.statusCode, 201);
                test.equal(res.headers['content-type'], 'text/plain');
                test.equal(res.body, 'created');
    
                logger.stop();
                test.done();
            };
    
            write(testmsg, options, write_ready);
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.