paperbeatsrock
paperbeatsrock

Reputation: 41

Jasmine test on javascript from file in another directory

I am trying to write a unit test suite for a javascript file that is located elsewhere in the project directory.

I cannot relocate that file or the spec file due to certain other dependencies, how can I import it?

here's how the project directory structure looks:

MYFUNC.js looks like this:

var constants = {a:100}
var myObj = function(){
    var local_var = true;

    myObj.doSomething = function(){
        return true;
    }
    myObj.answerToEverything = function(){
        return 42;
    }
}

This is my goal test case in MYFUNC_tests.js

describe("MYFUNC ", function(){
    var obj_a = myObj();
    it("Universe question ", function(){
        expect(obj_a.answerToEverything).toBe(42);
        done();
    }
});

Any help or links to guides on correct importing would be greatly appreciated!

Upvotes: 0

Views: 1081

Answers (1)

Sami Almalki
Sami Almalki

Reputation: 628

Try this hack:

var myFile = require('myFile');
var myFunc = myFile.readFileSync('./js/module/MYFUNC.js','utf-8');
eval(myFunc);

You should have node.js

Upvotes: 1

Related Questions