Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external variables in tests? #37

Open
ajoslin opened this issue Dec 30, 2013 · 3 comments
Open

Support external variables in tests? #37

ajoslin opened this issue Dec 30, 2013 · 3 comments

Comments

@ajoslin
Copy link

ajoslin commented Dec 30, 2013

I love autodoc, I love the inline testing and doc generating, and I love how it encourages me to write simple tiny testable functions :-)

One question I have is: is there a way to allow external variables in a test?

Eg one of my functions uses _.filter, and I can't figure out how to make autodoc's testing find/have _.

Am I missing something or is this just not a feature yet?

@dtao
Copy link
Owner

dtao commented Dec 30, 2013

Thanks for the kind words! And thanks for trying out autodoc; as I'm sure you're aware, it is really early at this point and so you may find yourself encountering bugs and weird behavior.

As for this issue, the answer to the general question is: You're not missing anything; external variables is just a tricky thing I haven't 100% figured out how to deal with yet (though I have some ideas). I.e., I don't have a great mechanism in place for providing access within a @private function to variables that were declared outside of that function's scope.

That said, for this specific case, what you're trying to access can be marked as a global from autodoc's perspective and it should work. I.e., when running your tests use the --globals flag:

autodoc -t --globals _:underscore your_file.js

The --globals option accepts a comma-separated list, where each item looks like variable:library (so in the example above the variable _ will be assigned to the result of require("underscore")).

Note that this only works as of 0.5.2, which I just published. (In 0.5.2 the --globals option expected all references to be to relative paths, which didn't make sense in retrospect.)

@ajoslin
Copy link
Author

ajoslin commented Dec 30, 2013

Awesome, thanks :-)

Just tried the --globals and I think I found a bug: since I'm using autodoc cli, it's trying to require() my modules from /usr/local/share/npm/lib/node_modules/autodoc/bin, and it gives Cannot find module 'lodash' error.

Here is my command with the error:

autodoc --test --globals _:lodash helpers/*.js

I fixed it by setting NODE_PATH:

NODE_PATH=$PWD/node_modules autodoc --test --globals _:lodash helpers/*.js

Not sure how you can set NODE_PATH programatically or I would open a PR.

@dtao
Copy link
Owner

dtao commented Jan 3, 2014

Yeah you're right, I think I kinda botched that. I guess for now it will only work with packages you've installed globally w/ npm install -g. I'm glad you found a workaround, though! As for the proper fix, I'll look into it and close this issue when I figure out the right approach.

dtao added a commit that referenced this issue Jan 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants