Access for global variables JavaScript unit testing - Stack Overflow

Hello I am new JavaScript unit testing and I'm using Mocha.js and Chai.jsWhat I want to do is simp

Hello I am new JavaScript unit testing and I'm using Mocha.js and Chai.js

What I want to do is simply figure out how to check the value of a global variable in a seperate js file. Here is my code

Here is the main.js file (code to be tested) Just has the variable I want to test against.

//main.js
var foo = 9;

Here is my test file

var assert = require("assert")
var expect = require('chai').expect
var fs = require("fs")
var vm = require("vm")

function include(path){
    var code = fs.readFileSync(path,'utf-8');
    vm.runInThisContext(code,path);
}

describe('Global', function(){
    include('lib/main.js');
    it('Should check if value is matching', function(){
        expect(foo).to.equal(9);
    });
});

Again, I'm new to unit testing in JavaScript. Any help will be greatly appreciated. The error I get back is foo is not defined which tells me that it can't access the variable, so how can I access it? Thank you in advance for the help.

Hello I am new JavaScript unit testing and I'm using Mocha.js and Chai.js

What I want to do is simply figure out how to check the value of a global variable in a seperate js file. Here is my code

Here is the main.js file (code to be tested) Just has the variable I want to test against.

//main.js
var foo = 9;

Here is my test file

var assert = require("assert")
var expect = require('chai').expect
var fs = require("fs")
var vm = require("vm")

function include(path){
    var code = fs.readFileSync(path,'utf-8');
    vm.runInThisContext(code,path);
}

describe('Global', function(){
    include('lib/main.js');
    it('Should check if value is matching', function(){
        expect(foo).to.equal(9);
    });
});

Again, I'm new to unit testing in JavaScript. Any help will be greatly appreciated. The error I get back is foo is not defined which tells me that it can't access the variable, so how can I access it? Thank you in advance for the help.

Share asked Feb 13, 2015 at 23:17 salcesalce 4071 gold badge12 silver badges29 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

var foo = 9; does not declare a global variable, it declares a local variable. In Node.js, a local variable declared in the outermost scope of a module will be local to that module.

If you want to test the value of a local variable declared in another file, your best bet is probably to read the contents of that file into a string (using fs.readFileSync, perhaps) and then eval() the string, which should define the variable in the current scope.

That will only work if the local variable is declared in the file's outermost scope. If it's a local variable inside a function, for example, you're out of luck (unless you want to do some gnarly string parsing, which would stretch the bounds of sanity).

Your global object is usually window

a global var foo = "test"; is the same as window.foo = "test"; or window['foo'] = "test";

Window is not defined when mocha is run in node, but this blog post uses "this" bined with a self-invoking function to get the same result.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744813838a4595206.html

相关推荐

  • Access for global variables JavaScript unit testing - Stack Overflow

    Hello I am new JavaScript unit testing and I'm using Mocha.js and Chai.jsWhat I want to do is simp

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信