-
-
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
Expose beforeAll/beforeEach/afterAll/afterEach in require'd files #764
Comments
I've been running into this lately, I see that #286 tried to fix this but was a bit too magic. I have a slightly simpler proposal.
It'd end up looking something like this // ./spec-helper.js
require('mocha').current.on('run', function() {
beforeEach(function() {
whatever()
})
}) Any thoughts? |
regular require() works fine |
Regular For proof of how difficult this is to get the actual running version of Mocha, look no further than @domenic's (now deprecated) mocha-as-promised which attempts to override mocha behaviour, but first has to fish inside require's cache to find the real running version of mocha domenic/mocha-as-promised@7714899#L4-L18 However, that is an aside from the main issue, which is that it would be useful for plugin authors to have hooks available for test suites, say for example when each Suite runs. |
👍 for this. It seems to me that files loaded with |
I still don't understand why this is necessary. I set up fixtures in a separate module and I'm going to need to see a concrete example of how |
My take is that it is a convenience feature that: It's a common pattern in Ruby's Since I've started writing smaller modules, this hasn't affected me so much - but for larger projects with larger teams I think it provides some benefit. |
@boneskull I've detailed my use case as part of the initial issue, but I'm happy to explain again: I want to use Sinon's Sandbox feature to integrate with Mocha (so I can use Mocha does not provide a particularly friendly interface for this. Using In summary, the sinomocha library I have, which is a pretty reasonable usecase and if Mocha had the right tooling, could be defined in about 20 LOC, is instead a convoluted hack 10 times the size because Mocha makes it difficult to write plugins. Admittedly I haven't revisited the situation in the 9 months that have passed since I filed this issue - so I don't know how much of what I have said has changed. |
I should have been more specific--I need to see code, because as TJ originally wrote, // my-fixtures.js
module.exports = {
before: function() {
this.sandbox = sinon.sandbox.create();
},
after: function() {
this.sandbox.restore();
}
}; var fixtures = require('./my-fixtures');
describe('foo', function() {
fixtures.before.call(this);
fixtures.after.call(this);
it('should blah', function () {
// ...
});
}); If that's too much: // required.js
var sinon = require('sinon');
global.sandbox = null;
global.myBefore = function () {
global.sandbox = sinon.sandbox.create();
};
global.myAfter = function() {
global.sandbox.restore();
}; describe('foo', function () {
myBefore();
myAfter();
it('should blah', function () {
// ...
});
}); The above will necessitate I still don't get why
Agreed. A plugin API is the top priority feature right now. As far as "convenience" features are concerned, we can't really accept PRs for these at the moment due to lack of resources. The plugin API would make it easier to for users to develop these features themselves. I'll have to investigate if it's feasible for |
I think a proper plugin API would enable this to work - assuming a plugin is arbitrary code that can be hooked into the mocha lifecycle. There's a PR above with a sketch of how this might work API-wise. |
Yes, a plugin API would work nicely for this and other use-cases. We could use @boneskull's examples, but I am after a seamless/transparent experience where requiring the plugin and loading it sorts everything, no user intervention required. I like the ideas @glenjamin mentioned in the second comment @boneskull if you're struggling with a lack of maintainer time, I am happy to help out where I can - either stewarding the issue tracker, or reviewing PRs, as I do for Docco, Chai, and Node-Sass. I use Mocha on a daily basis so can dedicate some time to it. Feel free to email me (work at keithcirkel dot co dot uk) to discuss further. |
what sort of feedback is still needed here? would love to see this. at the moment, i have to use |
Actually, there's another situation where this would be really useful. I'm using Wallaby to run my tests in parallel. For this to work with the integration tests I've to use a separate database per test runner (I simply append the process id to the database name). This is done in The issue I'm having is: how can I drop that temporary database when the test is complete? I can't use |
I have a lot of use-cases of this feature.
|
@eagleeye thanks for some good specifics |
It's a bummer that this isn't supported (easily) by the mocha CLI. I've been spoiled by gulp-mocha, which lets you do... gulp.src(['path/to/setup/*', 'path/to/unit/**/*'])
.pipe(...) Aside from changing the behavior of the -r flag, I prefer the API of Gulp. Although the syntax isn't as nice, I've recreated that behavior using the CLI by changing my glob:
I figured I'd post the option in case anyone else likes it more than the other workarounds suggested above ✌️ |
This is exactly how I solved it as well in my tests. Removed the mocha test/client/setup test/client/some-test |
The issue with I have "test" script in package.json similar to this
I found that adding "test/setup.js" to mocha.opts (as a last thing) is working well, not sure if it's by design, but it works with mocha 3.2.0 |
@evgeny-myasishchev if you only want to run one test, isn't that what |
@tbranyen Yep, I'm using those options as well. For me it's quite often just faster and more flexible to use Also with only there is another issue if you use it with --watch. If you start some specs in a watch mode and "only" and then remove only, then mocha will "see" zero tests so I have to restart it. |
@evgeny-myasishchev I hate that issue so much. If I could fix one thing about Mocha, it'd be that watch bug. |
Looks like someone has nearly done this #2544 |
I am a bot that watches issues for inactivity. |
Ahhhhghhhhh!!! I am new to Mocha and according to the documentation this is supposed to be a cinch. I thought that it should just work in required files. why isn't this working? I have read the comments but still don't get why you guys think that exposing these hooks is bad. |
For the record, Mocha should run files like this:
For example, if you have six files named 1.js through 6.js that need to run from 1 to 6 with the first three not having 3.js
--require 1.js ...and this in "test": "mocha 5.js --require 2.js" ...and calling this on the CLI: npm test -- 6.js --require 3.js (Note that I've reversed the order of That should provide more than enough flexibility to satisfy all reasonable use cases; as far as I can think of the only possible cases it doesn't cover (of which I'm not sure there is any value) are:
It might be worth adding a small section to the documentation to highlight this relationship of |
I'm amazed this is still a problem. It seems to me a pretty common use case to: A) have a test setup file, which does thing like instantiating a Sinon sandbox B) want to invoke mocha tests multiple ways; you might run C) not want to have to repeat boilerplate such as Given those (IMHO pretty sane) requirements, The only problem is, if you try to use It would be really convenient if Mocha just made the globals available to const doTestSetup = () => doWhatever(); You can also use Hope this helps others. |
No, this is not still a problem; use |
Nevermind, I was confused by build tasks. When I run my tests with I suppose I could do ... Is there really no way to run code with the mocha globals exposed before your tests? |
Ensure you've upgraded to a version of Mocha with // test.spec.js
it('should be ok', function () {}); // setup.js
afterEach(function () {
console.log('a test happened');
}); $ mocha --file setup.js test.spec.js
✓ should be ok
a test happened
1 passing (6ms)
|
I just updated Mocha to make sure I wasn't being stupid :)
Here's a real simple example: A.js
ATest.js
testSetup.js
You should get:
|
Mocha doesn't support |
Oh, should have mentioned that I'm using Babel, so it's all getting transpiled down to |
Sorry, didn't see your Mocha version. |
Ah! I thought doing
and when I run my example again I get ...
...still. It's just three files, two of which are one-liners; any chance you could try it on your end? If it works for you I'm fully willing to blame user error/my own stupidity. |
what does |
WTF! 😳 I just retried Sorry for the confusion; not sure what's going on in my environment but it clearly doesn't appear to be Mocha's problem. Thanks a bunch for all of the help. P.S. I guess I was using an (outdated) global mocha without realizing it. When I switched to: |
It's pretty simple; |
Can you pliz explain:
there is a
Somehow Please try to explain how mocha runs things, how it works with our tests in the documentation. |
Part of the utility of having mocha require in a common .js file (
--require
) is to be able to set up some common convenience features for ALL tests.It would be useful to have some hooks similar to, or exactly, the /(after|before)(All|Each)/ hooks.
Use case: Being able to integrate tools like sinon more deeply with mocha, allowing automatic tear-downs of stubs/mocks for each/all steps.
The text was updated successfully, but these errors were encountered: