-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
🚀 Feature: opt-out of globals #956
Comments
Even if it just dumped its crap all over the global space when you |
I don't think it's silly but there are tradeoffs with everything. I'd be happy with something like this: var Mocha = require('mocha');
var mocha = new Mocha(options);
mocha.describe('blah blah', function.... no one would use it but at least it would be a cleaner way to implement what we currently have. There would be a ton of boilerplate that everyone would have to set up each time, but if we could narrow those down to CLI-ish APIs that would be ok. Even if there was lib/cli.js that just passed in the ARGV, but I still doubt anyone would use it, you can use it without the CLI reasonably easy, but that illustrates that no one really wants to beyond some edge cases. |
@visionmedia that seems pretty nice. The reason I suggested I may try a pull request for this one day. |
This is a great example where a little future JavaScript via destructuring assignment goes a long way.
Just wish there was node harmony support for this. |
Is the idea of this to be able to do
And have it run that test? |
@glenjamin yup |
Having tried to do this myself on nodespec, the problem wasn't really making the various functions available, the problem was figuring out how/when to begin executing - we'd still need a two-pass define/run approach. The options I came up with were:
(1) is probably the nicest, which could look like this:
This doesn't seem to add much over doing |
I think this no longer fits with how mocha currently is used. As this thread has been inactive for over a year, I'm closing it for now. If anyone is still interested in this, feel free to comment or propose a pull-request and we'll consider :) |
Could this be reopened? I'm still very much interested in it. Not being able to just Edit: the "node-ability" argument was elaborated by @tmcw here. |
For clarity, the lack of a
And it gets nicer with ES6:
The issue is the fact that this only works when running via the |
Oh, thanks for the clarification. :) If we already offer this ability when ran using the mocha binary, I agree it'd be nice to do the same with node. Will look into it. Thanks! |
Now that node has a beforeExit hook, this seems fairly doable. Would need to be something like var { describe, it } = require('mocha/auto') |
Yeah. As TJ said, the problem is even if we were to make everything "node-able", it may require too much boilerplate to be useful. However... @glenjamin thanks for the heads-up on |
I've added a proof-of-concept to 3cdd4a0 |
anyway, it would need a bit of work, but I think we can easily generalize it for all interfaces. there is no support here for Growl, $ node test/my-test.js --reporter=dot If anyone has any suggestions, lemme know |
I think extracting some of the stuff in I'm still not sure this is really a useful addition though? Is there much of a practical difference between any of the following? node test/test.js
./node_modules/.bin/mocha test/test.js
PATH=./node_modules/.bin:$PATH mocha test/test.js
npm test -- test/test.js I get the feeling that people who aren't using mocha because they can't run test files via |
@glenjamin it kind of just needs to happen. Mocha should have a core programmatic API. A nice side-effect of that is node-able tests. The environment in which it runs will consume the programmatic API, whether that's a browser, CLI, or something else entirely. Mocha.app, anyone? :) |
Interested of this feature! |
+1 for: |
Watch mode breaks otherwise. See mochajs/mocha#956.
Ripped out Mocha because it doesn't let you import `describe` mochajs/mocha#956 and mocha-loader didn't work and come on, they should export `describe`.
Ripped out Mocha because it doesn't let you import `describe` mochajs/mocha#956 and mocha-loader didn't work and come on, they should export `describe`.
While I know nothing about how mocha is structured, I can assert that this single design choice is the source of the problem. |
likely so! |
I actually took a second look at the code, figured out (more or less) what I'd done wrong last time I'd tried to hack around having globals, thought about what it would take to get test files runnable with
Thoughts? |
Actually, on further thought, I motion to close this ticket, on the following rationale against using
Should we discover any significant advantage that is easier to achieve in core than outside of it, we can always reopen. However, I also motion to reopen the avoiding-globals ticket, because:
|
so you’re suggesting rather that Mocha provides a way to run tests with |
part of what makes mocha mocha and part of its success is that it doesn’t require boilerplate to import stuff. despite the dogma around polluting the global namespace there are completely valid reasons to do so for developer ergonomics. |
Yup.
I'm not saying I disagree in general, but as an opt-in feature, I can see where some developers may want imports instead of globals (maybe they want their module to name a local variable (Plus we're already maintaining a kinda-sorta attempt, where after the interface is set up on global it's copied onto the This is IMO in contrast to being able to |
@stevenvachon
|
@zapphyre you still need to run the test suite via the |
You can't run multiple source files with Updated the issue title to be more specific |
Another reason for this: I would like to run tests using a different JS runtime, for example low.js (because it's one of our platforms, others are node.js, browser - and React Native). I can do that when I have a JS file that imports mocha and runs the tests, I can't do that when I have to call I mean, you already do it for the browser. On that platform I have JS code that imports "mocha" and then runs the tests from a global In the browser I
And that's it. When I try the same in node.js, using a require statement instead of SystemJS to load a test file, I get (using mocha 5.2.0)
EDIT: I tried https://github.com/mochajs/mocha/wiki/Using-Mocha-programmatically and |
Is there currently any way to import describe/it? |
There is a new (as of now 20 hour old) Wiki page https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically but that is for running the tests. |
If the alternative runtime has a node-like executable (which I think low.js does?) then you can do |
https://github.com/tapjs/node-tap/#tutti-i-gusti-sono-gusti |
Now that #3006 is resolved it would be nice to have the same named exports in the browser. Currently, the following is working: <script type="module">
import './node_modules/mocha/mocha.js'
console.log(mocha)
</script> But the following is not: <script type="module">
import { mocha } from './node_modules/mocha/mocha.js'
console.log(mocha)
</script> (as requested by #956 (comment)) |
This might become easier when we merge our branch with roll-up and get transpilers set up. It seems feasible to have an esm bundle and a legacy bundle that consumes the esm code paths and adds the global leaks. We'd have to take a lot of care to only make changes in newly added code paths and bundles so we wouldn't break or code both in browsers and node |
With global vars and typings this just makes mocha legacy thing. |
Btw just created Hope they (globals) just will be removed officially soon. |
If you use |
As requested in #2210 I would like to suggest the proposed solution as a way to opt-out of globals.
Right now only one of the following interfaces can be defined via the |
@isaacs is complaining that Mocha tests aren't
node
-able. I think this is a silly complaint, but I think he represents a vocal minority, so shutting them up would be possibly valuable.Here is what I envision: instead of being able to use e.g.
describe
,it
, etc. by default, you'd doAlternately it could be just
and that would set up some globals for you.
What do you think?
The text was updated successfully, but these errors were encountered: