I am using jasmine-node to run jasmine tests. My project has a typical structure with "spec" and "src" folders.
Inside my "HelloSpec.js" file I had:
require('../src/Hello.js');
However when I ran the tests from the project's root folder (ie, the parent folder of both "spec" and "src") with this command:
jasmine-node spec/HelloSpec.js
I got errors indicating that the require
d files had not actually been required. But If I changed the require statement to:
require('src/Hello.js');
everything worked fine. So it seems that the require statements were resolving the paths relative to the folder where I was executing the tests, rather than relative to their own file location. But it doesn't make sense to me that it would work like that.
How are relative paths in "require" supposed to work? Do I need to do something to make them work the way I expected them to?
Thanks!
Usually a path from a current directory starts with a ./
which means current directory. What you have with your src/Hello.js
is a path search which seems to include the project folder.
I was of the understanding that the require ./
and ../
is relative to the file that it is being required from or at least this has always worked for me. if it doesn't start with either of these then it will usually try a path search.
jasmine-node is not node which is what my experience is based on.
jasmine-node may have a diffrent path definition which is why the path search seems to work.
If its not broke don't fix it? :)
Problem turned out to be me being careless. I'd forgotten to do module.exports. I fixed that and now everything works the way I expected. Not sure why it was working the other way without any export or module.exports, but it was.... Anyway, if you want to edit your answer to include this comment, I'll accept it