A module that support both command line and exports itself - node.js

Is there a clever way to create a module that can both be run with index.js arg1 arg2 as well as exporting itself to be used by others, ie. module.exports?
Say I have a script that kickstart a proxy server, while I can do proxy.js host port, I can't think of an easy way to export it as well. Should I look at process.argv to decide whether to exports or run itself? Or is there a better pattern?
PS: I was trying to write test cases for it but realize without module.exports, it's kinda hard to provide proper test coverage. Just curious how one would tackle this.

Related

How can I load a global script (<script src="">) in Jest

I need to make a JavaScript script available as a global script tag for a unit test in Jest.
I understand that Jest uses jsdom and in jsdom there is an api that allows to initialize the DOM and load script but I cannot seem to find a way on how to do this in Jest?
If you do not need to test the script itself, which, given the fact it is an external library, you shouldn't, you can mock it globally.
First of all you need to setup a jest setupFile, this file will be executed before test execution and the mocks available there should remain available for each test file, to do so write something like this in package.json or, better, in jest.config.js
"jest": {
"setupFiles": [
"<rootDir>/jest/globals.js"
]
}
Then create that file and write your global mocks there.
Now you will say "'Duh'? How do I mock the external library imported through the tag script?" Well, what I found useful for this and a number of other reason is to wrap the imported library in a separate module, this way you can use the wrapper throughout your code instead of using directly the globally imported external library, which will give you the possibility to mock the said module globally and voilĂ , it is done!
You can simply use require to load your script.
However, keep in mind that the script must be saved locally.
require('relative/path/to/your/script')
Some more interesting information here :
https://info343.github.io/jest.html

Running doctests using a different function

I wrote a module with a few functions along with their doctests, and I would like to run these tests on functions with the same names but written by someone else.
The documentation provides the following snippet for retrieving all tests for somefunction in mymodule and then running them in the usual way (like running doctest.testmod()):
TESTS = doctest.DocTestFinder().find(mymodule.somefunction)
DTR = doctest.DocTestRunner(verbose=True)
for test in TESTS:
print (test.name, '->', DTR.run(test))
But I don't know where to go from here to have those tests run on theirmodule.somefunction instead. I tried changing the filename field from mymodule to theirmodule in the Example objects for each test, but to no avail. Does anyone know how to achieve this?
This may not be the most elegant solution, but simply copying my docstrings to their functions in my script works:
theirmodule.somefunction.__doc__ = mymodule.somefunction.__doc__
And then I only need to run the snippet in my question on theirmodule.somefunction.

Avoiding require(../../../..) relative paths with grunt mocha

When unit testing, I tend to have a directory called test at the top of my project structure, with the directory structure mimicking the source code that is to be tested. However, these directories can get quite deep, for example
app/src/js/models/User.js
with perhaps a test in
test/app/src/js/models/User.js.
Now, when I want to include the User.js module, I use require('../../../../../app/src/js/models/Users.js') which is very cumbersome.
Ideally, I would like to use require('/app/src/js/models/User.js') or perhaps even require('User.js').
Is this possible? I am using grunt-mocha-test, but I think the question is a more general one.
There are multiple options you could use. Your best bet would probably be to use some npm module, for example this one. Search the npm registory for require, there are tons of options so choose whichever suits your needs.
Alternatively, you could write some helper function that does something similar.
If you were looking for some native way (built-in to Node.js) to achieve this, sadly there are none. You will have to use either a custom function or an npm module to do this in some nice, reusable way.

Difference between lib and lib-cov in express?

I'm new to Node.js.
module.exports = process.env.EXPRESS_COV
? require("./lib-cov/express")
: require("./lib/express");
I know EXPRESS_COV returns a Boolean value, but what is the difference between lib-cov/express and lib/express?
process.env.EXPRESS_COV would be true when you're running tests and want to see the code coverage of those tests (i.e. how many lines of your codebase are actually executed when the tests are run). Mocha, the test framework used for express, achieves this through the use of jscoverage.
JSCoverage parses through your source code and adds a bunch of lines that look like this:
$_jscoverage[filename][line]++;
Naturally, that's rather confusing to have in one's source code, not to mention adding a lot of bulk. So we'd never want JSCoverage processed files in our codebase. Fortunately, JSCoverage places the modified files in a different directory. In this case, ./lib-cov/ instead of ./lib/. That way, we can see how effective our tests are and not clutter up our code.
For details on how this whole rigamarole runs, see TJ Holowaychuk's article.
If you want to avoid all of this, you can use Istanbul instead, as it's much simpler and doesn't require exceptions in index.js

How do you access the "special-keys" module from an Intern functional test?

How can you access the "special-keys" module from an Intern functional test?
I've tried specifying the path in the opening define statement of the test, I've also tried updating the "loader" attribute in the intern.js configuration file, but with no success so far. This must be a fairly common use case for a functional test but I've not had any luck so far and can find any examples in the source or the Git Wiki.
In Intern 1.3+ you may access that module through intern/dojo/node!wd/lib/special-keys. Alternatively you may simply use the escape codes directly in your type/keys strings.

Resources