Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

No way to run code after all tests complete #241

taxilian opened this Issue Jul 2, 2013 · 11 comments


None yet
8 participants

taxilian commented Jul 2, 2013

I am not looking to run testing code; I simply need to close the database connection so that the process will end. there seems to be no way to do this, and this is a pretty critical fatal flaw for using jasmine with node.js


tebriel commented Jul 2, 2013

Why not use an afterEach in your describe block?

taxilian commented Jul 2, 2013

because I need to close the connection after all of the unit tests finish, not just after each describe block. Mongoose is designed to open one connection for the whole process, use it, and close it when everything is done.


tebriel commented Jul 2, 2013

Well, first thing off the top of my head (not ideal) is you can use the force exit flag when you run jasmine node. I'll think about other solutions today.

On Jul 2, 2013, at 12:27 PM, "Richard Bateman" notifications@github.com wrote:

because I need to close the connection after all of the unit tests finish, not just after each describe block. Mongoose is designed to open one connection for the whole process, use it, and close it when everything is done.

Reply to this email directly or view it on GitHub.

taxilian commented Jul 2, 2013

I wasn't aware of that flag; good thought. The other idea a friend of mine thought of was to write a custom reporter to execute the code. Kinda hackish, but likely effective. I'll try it tonight.


tebriel commented Jul 2, 2013

Lemme know how that goes.

This worked for me. I added the following code to a helper module:

 * Override the finishCallback so we can add some cleanup methods.
 * This is run after all tests have been completed.
var _finishCallback = jasmine.Runner.prototype.finishCallback;
jasmine.Runner.prototype.finishCallback = function () {
    // Run the old finishCallback

    // add your cleanup code here...

Great suggestion sobotkip - it was just what I needed. I guess we really need an afterAll() teardown method as part of the framework.

I can't believe we need to monkey-patch Jasmine to get basic setup and teardown hooks. This issue has been known for years and it doesn't look like Pivotal's gonna budge: pivotal/jasmine#56. I wanted to use jasmine but after butting my head against the same problem of needing to stop an Express server, I decided to switch to Mocha which worked right out of the box.

tdumitrescu added a commit to tdumitrescu/fast-and-pointed-brunch that referenced this issue Nov 21, 2013

Forget about jasmine, going with mocha+expect - we need a way to perf…
…orm setup and teardown to stop the Express server after tests are done, and it doesn't look like there's going to be a way to do it in jasmine without monkey-patching the framework: mhevery/jasmine-node#241

prantlf commented Jan 26, 2014

This is all about asynchronous tests, where the JavaScript code flow cannot be used to naturally place the setUp and tearDown sections. When a temporary resource needs to be created before the first test starts and destroyed after the last one finishes, an afterAll cannot be simply worked around, There can be scenario-dependent workarounds like those below, but it makes understanding the test code more difficult. There should be a single way to do this and the framework should offer it.

Some cases can be covered by artificial test steps, some by a beforeAll + afterAll plugin.

armw4 commented Feb 27, 2014

Am facing the same issue with jasmine-node and mongodb. I launch jasmine-node in a separate process via grunt.util.spawn, and it blocks indefinitely until disconnect is called. Problem is, I only want to call disconnect after ALL tests have finished, and not connect/disconnect per test. Like others of you have mentioned, beforeEach/afterEach are just not feasible for integration tests. Makes them much more expensive.

armw4 added a commit to armw4/auth-demo that referenced this issue Feb 27, 2014

Commit latest snapshot.
I've determined why the process blocks after all tests completes. It's
because you have to disconnect from mongodb, or the event loop with be
blocked. This makes sense, but what doesn't is why jasmine doesn't
provide hooks for beforeAll and afterAll. It's here where you'd want to
connect and disconnect from mongodb. Next step is to install the plugin
that monkey patches jasmine to provide this functionality.

* mhevery/jasmine-node#241
* https://github.com/nonplus/jasmine-beforeAll
* jasmine/jasmine#56

roselmamendes pushed a commit to roselmamendes/women-contributing that referenced this issue Jan 8, 2015

@roselmamendes roselmamendes referenced this issue in aninhalacerda/women-contributing Jan 8, 2015


Fix issue about close mongo connections during the tests. #7

zxqfox commented Oct 18, 2016

Am I right that we should close this? Issue is pretty confusing in opened state.

Related: #366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment