2

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 required 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!

Jonah
  • 15,806
  • 22
  • 87
  • 161

1 Answers1

1

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? :)

EDIT WITH CORRECT ANSWER

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

Jonah
  • 15,806
  • 22
  • 87
  • 161
Adam
  • 1,059
  • 9
  • 15
  • Hey Adam, thanks for your reply, but unfortunately it *is* broke. The require path needed to make jasmine-node work will break all the typical tools, such as browserify which is what I'll ultimately use to package up all my .js files into a single bundle.js to be included into the web apps where they will ultimately be used – Jonah Apr 26 '13 at 12:24
  • Ugh, problem turned out 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. – Jonah Apr 26 '13 at 13:10