问题
I am currently developing a project, which I want to test in different environments - including node.js and different browsers with karma/selenium - to avoid compatibility issues. (I think I will use browserify in browsers, but I am not familiar with it yet.)
I have a nested testing directory, something like this:
repo/
- project.js
- project.my.module.js
- spec/
-- helpers/
--- a.jasmine.helper.js
-- support/
--- jasmine.json
-- project.my.module/
--- ModuleClass.spec.js
-- project.MyClass.spec.js
-- project.OtherClass.spec.js
Currently I tested the project only with jasmine-npm (which is jasmine 2.2 for node.js). By testing the working directory is the repo/
, where I run node.exe
with jasmine.js
. The jasmine.js
loads the jasmine.json
:
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
Now I have 2 problems here.
- How can I avoid the long relative paths by require, for example
require("../../project.my.module.js")
in theModuleClass.spec.js
file? (I would rather use a short constant name, like I can do by symlink.) - How can I do this in a way that is compatible with running the same test files in different browsers? (I want to keep the commonjs module definitions by the tests.)
I checked some tutorials about node.js, and it seems like I have two options. I can use the package.json
(with some magic config options unknown to me), or I can move the files I want to load, to the node_modules/
(which I am sure I won't do). I am open for suggestions, because I can't see how it is possible to solve this issue...
edit:
The karma-browserify appears to solve the testing problem, probably I have to add a jasmine for browser, but that's okay. I don't have to change the commonjs module definitions by the tests. So it is possible to test both in node.js and the browser with the long paths.
edit2:
I ended up adding the parent dir of my repo to NODE_PATH. This way I can require every project I am currently developing.
回答1:
What about symlinking your project directory under node_modules
(e.g. as node_modules/project
) and requiring like require("project/project.my.module.js")
?
来源:https://stackoverflow.com/questions/28446269/how-to-avoid-long-relative-paths-with-jasmine-in-different-environme