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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:54:16+00:00 2026-06-16T20:54:16+00:00

This seems impossible (and it might be), but I’m trying to get into more

  • 0

This seems impossible (and it might be), but I’m trying to get into more TDD and I keep hitting a wall with closures. Say I have the following:

function createSomething(init) {
    function privateMethod(param) {
        return init[param];  //assuming this is more complicated, how can you test it?
    }

    function getData() {
        return init.data;
    }

    function getValue(name) {
        if (name === "privateNode" || typeof name !== "string") {
            return "permissionDenied";
        }
        return privateMethod(name);
    }

    return {
        getData : getData,
        getValue: getValue
    };
}

Putting aside this code probably isn’t the best illustration of my point and assuming “privateMethod” is something much more complicated than what is above, is there any way to run unit tests on methods like “privateMethod” or is the best you can do is to test the object created by createSomething? I ask because large parts of my application are hidden inside closures. I’m pretty uninformed in this area, but it seems to me that this is a weak spot for javascript and tdd.
fiddle for the code above without tdd is here: http://jsfiddle.net/QXEKd/

  • 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-06-16T20:54:17+00:00Added an answer on June 16, 2026 at 8:54 pm

    You can “smuggle” a function (or an Object of functions, variables, etc) out of your closure quite easily by adding an extra parameter to the constructor or hard-coding an early return. The smuggled function should still have the closure from where it was smuggled from unless you’re doing something like the evil eval.

    It might me worth noting that if you’re taking functions out like this for testing purposes only, it may be preferable to comment out or otherwise remove the code to access these before publishing it on your website, so it can’t be abused. Also, be aware that this will change depending on how a function is invoked, if you are using it.

    For example

    function createSomething(init, aaa) {
        function privateMethod(param) {
            return init[param];  //assuming this is more complicated, how can you test it?
        }
    
        function getData() {
            return init.data;
        }
    
        function getValue(name) {
            if (name === "privateNode" || typeof name !== "string") {
                return "permissionDenied";
            }
            return privateMethod(name);
        }
        // ----------------------------------
        if(aaa) return privateMethod;
        // ----------------------------------
        return {
            getData : getData,
            getValue: getValue
        };
    }
    
    var something = createSomething({
        privateNode : "allmysecrets",
        id : "1234",
        data : {
            stuff : "32t97gfhw3hg4"
        }
    }, 1); // passing extra arg to get method
    
    console.log(
        something('id'),
        something('privateNode')
    ) // 1234 allmysecrets
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a repeated question but i'm not able to get my answer.
Monitoring my global exception logs this error seems to be impossible to remove no
This seems a 'stupid' question, but let me explain. I'm working for a company
This seems to be primary Question. but i search it and i couldn't find
This seems like such a simple issue but I cannot find an elegant solution.
This seems to make perfect sense, but it is not working. I seem to
This seems like the simplest Git question, but I can't find ANYTHING on it.
This might be impossible to answer since there are probably too many variables here,
I'm trying to use DbLinq with a SQLite database, but I'm running into a
OK, this is impossible, but I will try to explain the situation here. Let's

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.