-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: pass command line opts through to browser #530
feat: pass command line opts through to browser #530
Conversation
The travis build is failing because the e2e test config file can't be found. I tried to fix the grunt task for this, but I've never used grunt before and can't figure it out. |
This is why we have |
Or with mocha, |
I know it depends on your test runner, but the workaround you describe covers far fewer cases than passing arbitrary arguments. For example:
These are all examples that work for the Oni Apollo test runner, which is what I'm using. I'm not expecting specific support or anything because it's obviously not one of the common platforms. But please, could there be some way to get some state / config passed from the running terminal to the browser running the tests? It doesn't have to be this patch if you don't like the changes I've make to the command line arguments, but without this feature (or something like it) you have to rely solely on what is present on on-disk files, which greatly limits interactive use on the terminal. |
@@ -140,34 +141,37 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file | |||
|
|||
// listen on port, waiting for runner | |||
var runnerServer = net.createServer(function (socket) { | |||
log.debug('Execution (fired by runner)'); | |||
socket.on('data', function(payload) { |
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.
this won't work for bigger data, it's a stream - you can receive the data in multiple events, you need to use json parser that can handle streams, or buffer it
Well, the main use case for Karma is to stay in the editor and run the tests on save, using auto-watch (also using auto-watch is more efficient than That said, I'm not against passing arguments into the browser. |
@gfxmonk I'm actually getting warmed on this... ;-) Why can't you keep the config file arg as it was ? |
Good to hear :) Assuming that the config was the first arg means that you must always provide the config as the first arg. Which is unnecessary when you're running Although looking at this again, it seems that the In which case, I can rework the branch to leave configFile as the first argument for |
Yep, at this point, the only option used for runner is the --runer-port, Anyway, even if we remove the config from There was also a discussion about exposing the whole config into the Maybe we could add "client" config, that would be passed to client. // in karma.conf.js
client = {
autoStart: false, // require.js etc. need to explicitly kick off the run,
captureConsole: true
}; From cli, you could do Don't know, just throwing some ideas... On Sat, May 11, 2013 at 9:18 PM, Tim Cuthbertson
|
But if you make the first argument to each command be the config, it means you have to pass an (unused and unnecessary) config argument to Aside from that, I agree that it should be explicit regardless of how args are parsed. So I've pushed a new version (to the same branch; should I be creating a new PR when I do this instead? Not sure of the etiquette here). It stops parsing at any '--' arg, and passes everything past that as "clientArgs", an array of strings. So there are no longer changes to existing use cases (i.e the config argument). I added tests in a separate commit, as I'm not sure whether the feature is important enough for a whole new e2e test. But it was useful for me, so I've added it anyway. Feel free to just take the first commit if you don't think it's worth its own e2e test. |
I needed this as well, and solved it a bit differently in #574. I just used underscore's "defaults" function to add any non-karma config properties back onto the config, then make sure the full config object was being sent to the client. I guess you could have collisions though if karma used the same property the adapter was looking for. |
I'd advise against passing a config object, as that forces optimist's |
The reason why I closed #574 and moved the discussion here is that it didn't address passing arguments from Can we list all the use cases for this feature ? I'm thinking about having "client" object in the config and that's what gets passed into the client. That means from the CLI you would do |
My use case is a test runner that works in nodejs and the browser. In nodejs, it parses command line arguments from I've added some minimal code in my test runner's karma adapter to inject the arguments array directly (as if they came from process.argv). But this only works with my fork, where the config passed by karma has an array of unmodified command line argument strings. I have test wrapper that looks a bit like:
So to run a certain test or set of tests across all platforms (node and the browser), I'll run e.g: So it's important (to me) that the options to node and via karma end up looking exactly the same to my test runner. Having a custom options object, or prefixed keys ( It seems to me that the lowest common denominator is argument lists - that's what all node-based runners will already be using. Making them deal with a different format makes it harder for them to integrate with karma. Having some other format (a pre-cooked options object, or prefixed options) is OK for runners that only work in the browser, but it's going to be frustrating for everyone who uses a runner that works in both node and via karma, because you're always going to have to write different arguments (despite the underlying options you're converying to the runner being identical). Aside from node.js compatibility, I don't like optimist's argument parsing. Karma can't know what options my runner expects in advance, so when it sees |
I'm not tight to optimist - if there's a better arg parser, I'm fine switching to it. Optimist was the best tool I found at that time. That said, it currently works fine, and there are other priorities ;-) @gfxmonk your node runner is not a typical use for Karma, I don't expect people to be using other runners with Karma. Should I ? What is the benefit ? So far I can see only one real use case for "passing args to the client" and that is filtering the tests, eg. #242 (comment) Other client config seems to be static (eg. At this point I'm leaning towards |
@gfxmonk that means, you would have to transform the args for your runner, however I don't think it's that big of deal. You already have a wrapper script anyway, so you can call this wrapper script with what you want: Also, I'm not sure, how you wanna filter the tests in the browser, using "unit/sequence-tests.js", unless you are using Require.js or something like that. |
client.x looks good to me.
|
I don't understand. Karma is not itself a test framework, so wouldn't most people be using "other runners"? My "runner" is not conceptually any different to mocha. Given that mocha takes arguments on the command line when running in nodejs, it seems entirely reasonable that it would want to do the same thing when running under karma. For example, if the mocha karma adapter were to support it, you might run:
To run the same tests in both node and the browser. requiring all options to be prefix with --client doesn't even make sense when you have non-option arguments (
I don't actually want to have a wrapper, so forcing a solution that requires pre-processing of options isn't actually a good thing. I haven't looked into it yet, but I'd ideally like to get rid of my wrapper and just become an adapter / karma plugin.
Yeah, it's a similar result to requireJS. Feel free to pretend that "unit/sequence-tests.js" is just an arbitrary string used to filter tests based on their description - the fact that it happens to be a file path isn't really important. There are (at least) two problems with prefixing everything with --client-:
Additionally, having the options pre-parsed into an object (rather than an array of strings) without knowing what options are expected is fundamentally intractable. Optimist is forced to make guesses because of this. For karma, those guesses happen to be good enough because it has very few options. For my runner (and for mocha) they are not. I gave an example of this in my last comment ( I feel like I've (already) listed a number of reasons for not wanting everything to be prefixed with --client-. And yet you still seem to be keen on the idea. Can I ask why? Are there good reasons for not wanting to simply pass everything after a "--" as an array? What benefit do we get by intentionally being less general? If you insist on prefixing client args with --client, then can we also pass all arguments after |
What is the benefit of running the tests with Mocha and Karma at the same time ? |
As in, running the same tests under both? To test code that is intended to run in a browser and in node.js. Perhaps app developers maintain two distinct test suites, but cross-platform library authors are sure to want to test the same code in all environments. |
@vojtajina so we good for |
I'm still waiting for a response to the multiple problems I've raised with the |
I'm scared of taking the whole space of everything after
I think we should expose the config in the same way as the MAPPINGS - inline into the I'd like to see input from more people, as this still sound very specific to @gfxmonk use case (no offense dude, I really appreciate all your contribution to this project and I do wanna solve this). |
Thanks for the points, Vojta. I understand that my use case is probably unusual, but it's frustrating to have something conceptually simple ("pass command line arguments") be made unusable by applying a lossy algorithm in front of it.
This is a fair concern. Although I disagree that it's only going to be used for filtering tests. There are plenty of other behaviours of a test runner that you might want to affect for a single test run during development.
I'm OK with your suggestion, except for the last point. You still haven't addressed my point that parsing arguments before you know the available options cannot be done in the general case. So I think while it makes sense to have (as you suggest):
send { "capture-console":false } The only valid treatment of:
Is to send Because pre-processing will always be a guess, and it will often be wrong (this is why I refer to it above as applying a lossy algorithm). Here are a bunch of examples off the top of my head where it will be wrong:
So sure, you can make a convenience API that --client.x arg gets transformed into |
That means your client code has to parse the arguments, but I think that makes sense. Can you please:
Sorry for delaying this PR so much @gfxmonk |
Thanks for coming around, @vojtajina :) I've rebased this PR / branch and addressed all of those checklist items. Making the e2e test run A minor concern is that |
@@ -18,7 +18,7 @@ var processArgs = function(argv, options) { | |||
|
|||
// TODO(vojta): warn/throw when unknown argument (probably mispelled) | |||
Object.getOwnPropertyNames(argv).forEach(function(name) { | |||
if (name !== '_' && name !== '$0') { | |||
if (name.charAt(0) !== '_' && name !== '$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.
now we don't need this change at all, please revert
@gfxmonk great, don't worry about the rebasing, I will take care of it. I don't know why Travis is failing, but it's probably because of the secured env vars don't work for pull requests, don't worry about that. |
All arguments after '--' passed to `karma run` are exposed in the browser as window.__karma__.config.args.
Nits addressed in cli.js. Yeah having secured vars available to PRs would probably be a bad idea. Thanks for taking care of the rebase. |
Yay! Any chance we could we get some docs updates to show how to use this? Also wondering how to use it programmatically w server.start and runner.run, which is what we use in grunt-karma. For the CLI I tried
|
@geddski at @vojtajina's request, command line args are only allowed for the So usage is:
This is documented in the As for
I'm afraid I'm not familiar enough with karma's docs to know where that should be documented though. |
You can define client config in module.exports = function() {
config.set({
client: {
args: ['--foo', 'bar'],
autoStart: false,
captureConsole: false
}
});
}; |
Ah, nice. Thanks fellas.
|
@vojtajina ok so with this, when passing the grep pattern, the Mocha adapter will receive |
perhaps we could have a raw |
@geddski I'm using https://github.com/trentm/node-dashdash with minimal changes (for potentially missing Array.prototype.map / forEach), which works fine if you've got something requirejs-like. It shouldn't be too much work to make it work without requirejs either, as it has only one dependency. |
Just realized I can parse the args in grunt-karma and pass them that way. |
Hi I can not understand how it work :( I start server
and then type grep
But karma execute all tests... what I'm doing wrong? I have karma 0.11.13 and karma-mocha 0.1.1 |
Now adapter get settings in array, like this __karma__.config.args = ['--grep test']; And karma-mocha not allow grep in latest version of karma |
@maksimr sounds like a bug in https://github.com/karma-runner/karma-mocha. Would you like to send a PR? |
@maksimr maybe try without the extra
|
@geddski thanks but it doesn't help |
@maksimr try to debug https://github.com/karma-runner/karma-mocha/blob/45553a286a9481ce01950b37e396eeb3ef66ca51/src/adapter.js#L83-L85 a bit, sorry I don't use mocha |
@maksimr ignore my last message, just saw your PR.... |
For large test suites, always running all the tests is slow. This change allows command-line arguments to be passed through to the runner as the single option to
__karma__.run
. This allows you to target specific tests per-run (when combined with a browser-side runner function that uses this feature).BREAKING CHANGE:
The config file is no longer assumed to be the first argument, it must be specified with either -c or --config-file. I had to do this in order for unknown arguments to be pass through unmolested.