-
-
Notifications
You must be signed in to change notification settings - Fork 306
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
Add flag to require modules before running tests #224
Conversation
The `source-map-support` module enables some rudimentary support for source maps in call stacks, which greatly enhances the developer experience of working with compiled code.
I don't understand what did does. Tape doesn't do anything with source maps. Why add it here ? |
My apologies, the description wasn't particularly clear. If the code being run is linked to source maps, then any errors thrown will have a call stack that reflects the original code. Without it, it's much more difficult to understand where errors originate. Does that help? |
Can you show me what a workflow looks like ? Would it be something like make build # compile my XJS -> real js
node build/test.js
# :( get bad stacktrace I think it might make more sense for some-one to write This way it's opt-in for people who want source map support and we don't have source map hooks enabled globally for all users of tape. |
That's pretty much the workflow, yeah.
Hmm, yes, maybe. The problem with more binaries of course is that they pile up. I want to run with source map support, but I also want to run a code coverage collector. But I think you're on to something though. Modules like make build # compile my XJS -> real js
tape node_modules/source-map-support/register build/test.js
# :) get good stacktrace Except, for whatever reason I can't seem to figure out right now, the above doesn't work. Would it be meaningful to add a tape -r source-map-support/register build/test.js I did a quick hack to test this, and it solves my issue of course. Speculating further, this kind of extension point could potentially enable things like running multiple pre-requisites: tape -r source-map-support/register -r collect-coverage build/test.js If one wants to parameterize these modules, or do something more esoteric, you can implement it in a local module: tape -r ./my-esoteric-prerequisites build/test.js Happy to change this PR, or make a new one, if you think this is within the purview of tape. Like you, I don't much like the added responsibility this PR puts on tape, which I think is brilliant in its limited scope and simplicity. Adding a |
I'm +1 for a Although demonstrating that this is powerful when it comes to for example, combining istanbul & tape together will help get it landed. If your going to add an options parser to implement |
Right on, thanks for the discussion @Raynos – much obliged! I'll carve out some time to implement |
This reverts commit 737aa42.
@Raynos accurately pointed out that the source map support was a bit too specific, and felt outside of the purview of tape. As a more general solution, this implements the `-r` and `--require` CLI flags, which will allow a user to load node modules before any tests are run. For example, if someon wants to enable source map support in node, they might do: tape -r source-map-support/register test/*.js
@Raynos ok I've removed the source map stuff and implemented Any thoughts? |
I updated the title and description of this PR to reflect the new changes. It makes the conversation look a bit weird, but whatever. :o) |
Some thoughts: Order is not preservedConsider the following: $ tape -r first second -r third Order is preserved for flags and globs separately, so the order of execution in the above example is $ tape -r setup-db test/*.test.js -r teardown-db Sure enough, this can be done without $ tape test/setup-db.js test/*.test.js test/teardown-db.js But this makes it more difficult to share such modules, for instance on a team that does a lot of similar work. I'm not sure which way to lean on this. Parameterizing modulesParameterizing modules can be done using environment variables: $ BABEL_ENV=production tape -r babel-register test/*.js Is there a need for parameterization of modules, that can't be solved with environment variables? If so, should the recommended approach be to implement a local module and load that? E.g.: $ cat ./my-esoteric-needs
var mod = require('some-module')
mod.installGlobablly({ some: 'parameters' })
$ tape -r ./my-esoteric-needs test/*.js |
I expect Working as expected & intended. If you want to feel warm and fuzzy then run |
You are spot on; use environment variables or write a local helper. |
Cool, thanks for the comments @Raynos. I think I agree with you re: the order of execution; it makes sense to run all requires first, regardless of what order they are specified in on the command line. I'll see about updating the docs as well before I consider my work here done. |
@@ -15,7 +15,9 @@ | |||
"glob": "~5.0.3", | |||
"has": "~1.0.1", | |||
"inherits": "~2.0.1", | |||
"minimist": "1.2.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use ~
This looks good to me. We need to add a section to the README about this new feature, i can do that after merging if you prefer. I think we should wait 24 hours before merging to let other reviewers like @substack take a look. |
This can probably be done better, but I think it covers all aspects of this feature.
Thanks for the review @Raynos, I've incorporated your feedback and added some docs to the readme as well. Feel free to review and suggest any changes, but I think that about covers it – what's your take? |
/* The `module &&` ensures we ignore `-r ""`, trailing `-r` or other | ||
* silly things the user might (inadvertedly) be doing. | ||
*/ | ||
module && require(resolveModule(module, { basedir: cwd })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd prefer if (module) { require(resolveModule(module, { basedir: cwd })); }
, rather than abusing boolean operators for control flow :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potato/tomato in my book, but happy to change it. Will push an update in a minute.
var glob = require('glob'); | ||
|
||
process.argv.slice(2).forEach(function (arg) { | ||
var opts = parseOpts(process.argv.slice(2), { | ||
alias: { 'r': 'require' }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason these "r"s are quoted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Force of habit, really. I'll change it.
Overall this LGTM! It'd be great to rebase this down to a smaller number of meaningful atomic commits |
Sure, if you decide to merge feel free to rebase any which way you'd like! :o) |
Thanks for the review btw @ljharb! |
Ideally we'd merge through the web interface which means you'd need to do any rebasing. |
With all due respect – I'm cool with contributing a feature and making sure the code is up to your standards so the patch can be landed, but the bike shedding is strong with this one. Regardless, I'll take @Raynos's cue and await any further feedback from @substack (or others.) |
Your contribution is appreciated, but what you call "bikeshedding" involves consistency with a project's style and commit history, and is at the least as important as new features. Certainly if you're unwilling to rebase the commits, and we'd still like the feature, we can either click merge and deal with the log clutter, or manually squash the commits ourselves, but to dismiss maintainer concerns as "bikeshedding" doesn't seem very helpful :-/ Awaiting further feedback seems best in any case. |
You're right @ljharb – my apologies. I didn't mean to offend anyone, nor dismiss feedback. I very much appreciate the solid feedback provided. |
This looks solid. I think the README is fine. As mentioned I'll wait ~24 hours before merging but if there's no strong objections I'll merge tomorrow. |
👍 i'll definitely be using this feature |
I've rebased and pushed a new version of this branch. I didn't want to mess with this branch, for reasons. Let me know how you'd like to proceed if you'd like to merge this. It's equally trivial to make the same changes to this branch, as it is to open up a new PR just for merging – your call. Thanks for the reviews and feedback! |
Add flag to require modules before running tests
Published 4.3.0 |
Much obliged! 👍 |
By adding the
--require
flag (and-r
alias) we allow users to require modules before running their tests. An example of this might be adding source-map-support, like so:$ tape -r source-map-support/register test/*.js
Another example might be to install the babel require hook:
$ tape -r babel-register test/*.js
Another potential benefit is the ability to more easily run external test suites:
Modules are found using the node module resolution algorithm, so if one wants to load a local module it is necessary to prepend
./
or../
, like so: