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

Add flag to require modules before running tests #224

Merged
merged 10 commits into from
Dec 23, 2015
Merged

Add flag to require modules before running tests #224

merged 10 commits into from
Dec 23, 2015

Conversation

mstade
Copy link
Contributor

@mstade mstade commented Dec 19, 2015

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:

$ npm install test-suite-rfc1337
$ tape -r test-suite-rfc1337

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:

$ tape -r ./my/local/module

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.
@Raynos
Copy link
Collaborator

Raynos commented Dec 20, 2015

I don't understand what did does.

Tape doesn't do anything with source maps. Why add it here ?

@mstade
Copy link
Contributor Author

mstade commented Dec 20, 2015

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?

@Raynos
Copy link
Collaborator

Raynos commented Dec 20, 2015

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 source-map-node binary that runs any node program but pre-loads source maps support.

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.

@mstade
Copy link
Contributor Author

mstade commented Dec 20, 2015

That's pretty much the workflow, yeah.

I think it might make more sense for some-one to write source-map-node binary that runs any node program but pre-loads source maps support.

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 source-map-support that have a kind of cross cutting module like register can just be loaded like tests would be. In the example above, it becomes:

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 -r,-require flag to tape, that would then enable this kind of workflow, yet still be opt-in? Something like:

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 -r,--require flag would make tape a little more complex, but I think the added value is potentially much greater than the cost of such a feature. What do you think?

@Raynos
Copy link
Collaborator

Raynos commented Dec 20, 2015

I'm +1 for a -r flag.

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 -r then please use minimist.

@mstade
Copy link
Contributor Author

mstade commented Dec 20, 2015

Right on, thanks for the discussion @Raynos – much obliged! I'll carve out some time to implement -r in the next few days, ideally before Christmas. I'll just repurpose this PR for it, but if you feel strongly otherwise feel free to close and I'll just open a new one.

@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
@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

@Raynos ok I've removed the source map stuff and implemented -r and --require instead. I also added a couple of tests for it, but I'm not sure of their quality. I've also not changed the documentation any, which is probably something that should also be done.

Any thoughts?

@mstade mstade changed the title Added rudimentary source map support Add flag to require modules before running tests Dec 21, 2015
@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

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)

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

Some thoughts:

Order is not preserved

Consider 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 first, third, second. Would it be more intuitive and meaningful to preserve the order entirely? Could this help with things like #126 where the node process never ends because of lingering handles? I.e. if order was preserved, then setup and teardown could be extracted into modules of their own, and common logic shared:

$ tape -r setup-db test/*.test.js -r teardown-db

Sure enough, this can be done without -r as well, by having setup and teardown files:

$ 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 modules

Parameterizing 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

@Raynos
Copy link
Collaborator

Raynos commented Dec 21, 2015

Order is not preserved

$ tape -r first second -r third

I expect first, third, second. Like first its going to run all the pre requires and then run all the glob patterns.

Working as expected & intended. If you want to feel warm and fuzzy then run tape -r first -r third second

@Raynos
Copy link
Collaborator

Raynos commented Dec 21, 2015

Parameterizing modules

You are spot on; use environment variables or write a local helper.

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ~

@Raynos
Copy link
Collaborator

Raynos commented Dec 21, 2015

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.

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

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 }));
Copy link
Collaborator

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 :-/

Copy link
Contributor Author

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' },
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@ljharb
Copy link
Collaborator

ljharb commented Dec 21, 2015

Overall this LGTM! It'd be great to rebase this down to a smaller number of meaningful atomic commits

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

Sure, if you decide to merge feel free to rebase any which way you'd like! :o)

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

Thanks for the review btw @ljharb!

@ljharb
Copy link
Collaborator

ljharb commented Dec 21, 2015

Ideally we'd merge through the web interface which means you'd need to do any rebasing.

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

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.)

@ljharb
Copy link
Collaborator

ljharb commented Dec 21, 2015

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.

@mstade
Copy link
Contributor Author

mstade commented Dec 21, 2015

You're right @ljharb – my apologies. I didn't mean to offend anyone, nor dismiss feedback. I very much appreciate the solid feedback provided.

@Raynos
Copy link
Collaborator

Raynos commented Dec 22, 2015

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.

@ljharb
Copy link
Collaborator

ljharb commented Dec 22, 2015

👍 i'll definitely be using this feature

@mstade
Copy link
Contributor Author

mstade commented Dec 22, 2015

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!

Raynos added a commit that referenced this pull request Dec 23, 2015
Add flag to require modules before running tests
@Raynos Raynos merged commit 62b352b into tape-testing:master Dec 23, 2015
@Raynos
Copy link
Collaborator

Raynos commented Dec 23, 2015

Published 4.3.0

@mstade mstade deleted the source-map-support branch December 23, 2015 08:34
@mstade
Copy link
Contributor Author

mstade commented Dec 23, 2015

Much obliged! 👍

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

Successfully merging this pull request may close these issues.

None yet

3 participants