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 6320365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:57:31+00:00 2026-05-24T15:57:31+00:00

I am trying to create a test suite for a module that I am

  • 0

I am trying to create a test suite for a module that I am writing in Node.js using Nodeunit. The module is a basic music playlist that allows adding and removing tracks to the playlist.

var playlist = function(){
    this.__playlist = [];
    this.__count = 0;
};

playlist.prototype = {
    addtrack:function(track){
        if(typeof track !== "object") throw new Error("Track needs to be an oject");
        this.__count++;
        track.id = this.__count;
        this.__playlist.push(track);
        return this.__playlist;
    },
    removetrack:function(trackid){
        if(typeof trackid !== "number") throw new Error("Pass in a numeric track id");
        var trackFound = false;
        for(var i=0;i<this.__playlist.length;i++){
            var t = this.__playlist[i];
            if(t.id == trackid){
                trackFound = true;
                this.__playlist.splice(i,1);
            }
        }
        if(!trackFound) throw new Error("Track not found in the playlist");
        return this.__playlist
    }
}

exports.playlist = function(){
    return new playlist();
}

As you can see there are places that throw errors based on incorrect data being passed in.

So here is my test suite.

var pl = require('./playlist');

exports.testPlaylistInit = function(test){
    var playlist = pl.playlist();
    test.equal(typeof playlist, 'object');
    test.done();
}

exports.testAddingTracks = function(test){
    var playlist = pl.playlist();
    test.throws(playlist.addtrack(), Error, 'Should fail for blank track');
    var track = {
        title: "Golly Sandra",
        artist: "Eisley",
        album: "Room Noises"
    };
    tracks = playlist.addtrack(track);
    test.equals(tracks[0],track);
    test.equals(tracks[0].id,1)
    test.done();
}

exports.testRemoveingTracks = function(test){
    var playlist = pl.playlist();
    test.throws(playlist.removetrack('Imma error'), Error, 'Show fail for non-numeric track id');
    var track = {
        title: "Golly Sandra",
        artist: "Eisley",
        album: "Room Noises"
    };
    playlist.addtrack(track);
    track = {
        title: ".44 Calliber Love Letter",
        artist: "Alexisonfire",
        album: "Alexisonfire"
    }
    playlist.addtrack(track);
    test.equals(playlist.removetrack(1)[0],track);
    test.throws(playlist.removetrack(10), Error, 'Should fail for non-existant track');
    test.done();
}

While writing the test suite I used test.throws as the assumption basically just wraps the code block in a try-catch statement and checks the catch against the error block. Apparently I am wrong though, because when I run the test with Nodeunit, Node displays the error message thrown by the module instead of the test suite catching the error. Am I using the test.throw case incorrectly?

  • 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-24T15:57:33+00:00Added an answer on May 24, 2026 at 3:57 pm

    Your usage of test.throws isn’t quite right. If you look at what you have:

    test.throws(
      playlist.removetrack('Imma error'),
      Error,
      'Show fail for non-numeric track id'
    );
    

    You are executing playlist.removetrack('Imma error'), and then passing the result of that to throws, so if there is an exception, it will happen before throws is ever executed.

    You should do something more like this:

    test.throws(
      function() {
        playlist.removetrack('Imma error');
      }, 
      Error,
      'Show fail for non-numeric track id'
    );
    

    You must pass in a function, that when executed, will try to remove the track. That way, your playlist logic is actually executed by the throws function, and thus can automatically be wrapped in a try/catch block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a unit test for a method that takes a
I am currently trying to create a test suite for my javascript apps. My
I'm trying to create an environment using virtualenv. virtualenv test New python executable in
hello i am using joomla and i am trying to create an option that
I am trying to create a function with a variant record-type parameter that allows
I am trying to create a test harness for our QA staff to test
I'm trying to create a unit test to test the case for when the
I am trying to create a unit test similar to how I would have
Specifically, I'm trying to create a unit test for a method which requires uses
Trying to create a user account in a test. But getting a Object reference

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.