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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:30:33+00:00 2026-06-10T22:30:33+00:00

The best way (IMO): test.add(Expect job not to be done., function(expect){ expect(job.done).toBe(false); }) This

  • 0

The best way (IMO):

  test.add("Expect job not to be done.", function(expect){
       expect(job.done).toBe(false);
  })

This question has been updated and I post here how I’ve ended doing the tests if someone might need it.

Original question below…

I know it might not even be possible but I will clearly explain why and how I want it to work:

Why?

I want to make some tests that doesn’t fail when something is missing and I wouldn’t want to test everything if exists or not.

Example:

expect('Something', existingClosureVar.notExistingProperty.toString()).toBe('ho ho');

This will throw an error like this: TypeError: Cannot call method ‘toString’ of undefined.

To work around this is quite easy, but also a pain!

if(existingClosureVar && existingClosureVar.notExistingProperty && existingClosureVar.notExistingProperty.toString){
     expect('Something', existingClosureVar.notExistingProperty.toString()).toBe('ho ho');
}

Well, but if it doesn’t exist I’m not even noticed about the failing test! Maybe some more detailed workaround might exists but would make this code bigger and bigger, when all I want is a simple thing, which should be the shortest thing possible.

How should work instead?

expect('Something', 'existingClosureVar.notExistingProperty.toString()').toBe('ho ho');

Somehow the expect function should have access to the variable local to the closure in order to make this work. It will run the string in a try-catch context and if fails something will be failing the test too.

Exact thing I want:

var callingFn = function(){
    var a = 99;
    // remember, the eval can't sit here, must be outside
    var evalString = 'console.log(a)'; // should print 99
    expect(this, evalString); // 
}
var expect = function(context, evalString){
        var fn = function(){
                eval(evalString)
        }
        fn.call(context);

}
new callingFn(); // creates a this in the callingFn that is not the window object

It works if I supply a context, but…

that would require me to use the “this.” notation to get a variable. As a lot of functions are async and the context of the functions is not maintained (could be maintained, but more work is needed) (or we could use a closure variable to keep the context variables).

Example:

var callingFn = function(){
    var context = {b: 1};
    var evalString = 'b'; // prints 1
    expect(context, evalString)
}
var expect = function(context, evalString){
        var fn = function(){
                console.log(eval('this.' + evalString))
        }
        fn.call(context);

}
callingFn()​

One ugly solution I found:

var callingFn = function(){
    // setup the context
    var context = {};
    // give access to the local closure
    context.fn = function(evalString){
        console.log(eval(evalString))
    }
    var a = 99;
    var evalString = 'a'; // should print 99
    expect(context, evalString);
}
var expect = function(context, evalString){
    context.fn(evalString);
}
callingFn()​

Workable solution but still too much verbose:

And also I must put 4 lines before the code, but here it is Click here to open the Fiddle example:

    var callingFn = function(expect){
    // give access to the local closure
    expect.fn = function(evalString){
        try      {return [undefined, eval(evalString)];
        }catch(e){return [e, undefined];}
    }


    var a = {hey: 1, b: {c: 99}};
    console.log(expect('a.b.c', 99)); // true
    console.log(expect('a.b.c.d.f', 99)); // return the error
    console.log(expect('a.b.c', 44)); // false
    console.log(expect('a.hey', 1)); // true
}
var expect = function(evalString, target){
    var result = expect.fn(evalString);
    var err = result[0];
    var output = result[1];
    if(err){
        return err.stack;
    }else{
        return output === target;
    }
}
callingFn(expect)​

Shared context:

Fiddle link

var callingFn = function(expect){
    // give access to the local closure
    var context = {};
    expect.context = context;
    context.a = {hey: 1, b: {c: 99}};
    console.log(expect('a.b.c', 99)); // true
    console.log(expect('a.b.c.d.f', 99)); // return the error
    console.log(expect('a.b.c', 44)); // false
    console.log(expect('a.hey', 1)); // true
}
var expect = function(evalString, target){
    var fn = function(evalString){
        try      {return [undefined, eval('this.' + evalString)];
        }catch(e){return [e, undefined];}
    }
    var result = fn.call(expect.context, evalString);
    var err = result[0];
    var output = result[1];
    if(err){
        return err.stack;
    }else{
        return output === target;
    }
}
callingFn(expect)​

Local var + string:

Fiddle link

var callingFn = function(expect){
    // give access to the local closure
    var a = {hey: 1, b: {c: 99}};
    console.log(expect(a, '.b.c', 99)); // true
    console.log(expect(a, '.b.c.d.f', 99)); // return the error
    console.log(expect(a, '.b.c', 44)); // false
    console.log(expect(a, '.hey', 1)); // true
}
var expect = function(object, evalString, target){
    var fn = function(evalString){
        try      {return [undefined, eval('object' + evalString)];
        }catch(e){return [e, undefined];}
    }
    var result = fn(evalString);
    var err = result[0];
    var output = result[1];
    if(err){
        return err.stack;
    }else{
        return output === target;
    }
}
callingFn(expect)​

// examples are for Google Chrome, it might not work in other browsers.

  • 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-10T22:30:35+00:00Added an answer on June 10, 2026 at 10:30 pm

    In a word, no. You simply cannot access a variable in a closure from eval code.

    Functions created with eval or the Function constructor always run in the global scope (or in strict mode, undefined scope). There is no way to avoid this.

    Furthermore, you cannot access a function’s scope from outside the function (or a function inside of that function). This is because JavaScript has static scope; scope is fixed at the time of declaration. A function cannot change its scope chain.

    If you just want your test to fail if a property is undefined, the simplest way might be:

    if (obj && obj.prop) expect('Some Test', obj.prop) ...
    else fail('Some Test');
    

    Or use a callback whose invocation is wrapped in a try.

    expect('Some Test', function() {
        return obj.prop;
    }).toBe('some value');
    

    expect can use the callback’s return value, or allow you to pass a simple value if you know that the identifier in question will always be defined.

    function expect(name, o) {
        var expectedValue;
        if (typeof o == 'function') {
            try { expectedValue = o(); }
            catch(ex) { /* fail here */ }
        } else expectedValue = o;
    
        ...
    }
    

    So you can call expect either way:

    function callingFn() {
        var localVar = 1, someObj = {};
    
        expect('Test var', localVar).toBe(1);
        expect('Test obj', function() { return someObj.missingProp.toString(); }).toBe('value');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Best way to illustrate my question is with this example code: class Item {}
The best way I think to explain this is to tell you what my
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
The best way to explain this is with code: byte roominspiration = 0; query
The best way to do this? Tried things like that: public String FormatColumnName(String columnName)
the best way center 2 divs inner div. Like this 1 2 3 4
For setting that I use Html helper method which is not the best imo,
The best way to explain this is with examples. Let's say I have the

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.