Reputation: 1323
I have a very simple question. How do I unit test a node.js class/function at a different directory location from my root specs folder?
What I mean here is, say I have a specs folder located at root of my project
/specs/test-spec.js
Here I have a unit test written in jasmine to a helloWorld.js class located in
/service/benefits/programs/california/helloWorld.js
Contents of helloWorld.js
var helloWorld = function(){
return 'hello world';
};
Here is my node-jasmine unit test
helloWorld-spec.js :
describe("Hello World", function() {
it("should return string hello world", function() {
expect(helloWorld()).toEqual('hello world');
});
});
The unit test won't pass because it cannot see helloWorld.js, how can I make it see helloWorld.js
Like should I add something like this:
var helloWorld = require(../../../../helloWorld.js);
I don't even understand what those dots represent? How can I make this work? Anybody? Please help.
Upvotes: 0
Views: 1037
Reputation: 6132
Your path is most likely wrong. The dots represent a path relative to the current path, every ../ takes it up one level in the folder structure.
Say your 2 folder structures are like this:
/root/specs/test-spec.js
/root/service/benefits/programs/california/helloWorld.js
if you want to access helloWorld.js in test-spec.js:
the reason you would use the ../
first is to go up one level, in this case from specs
folder into root
folder
the result would be: var helloWorld = require(../service/benefits/programs/california/helloWorld.js);
however if you were trying to get to test-spec.js from helloWorld.js you would need to traverse back through the folder structure to the root level which is again where the ../
comes in, as we need to go back into california, programs, benefits , service to get to root level:
var helloWorld = require(../../../../specs/test-spec.js);
This method is called relative paths, where the path is relative to where you are in the folder structure.Alternatively you can use the '/' character to indicate that you want to start at root level if you know your folder is always at root level:
var helloWorld = require(/specs/test-spec.js)
Hope this clears it up and that I understood your question correctly ;)
Upvotes: 2