jasmine-node icon indicating copy to clipboard operation
jasmine-node copied to clipboard

No way to run code after all tests complete

Open taxilian opened this issue 12 years ago • 12 comments

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

taxilian avatar Jul 02 '13 01:07 taxilian

Why not use an afterEach in your describe block?

tebriel avatar Jul 02 '13 16:07 tebriel

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.

taxilian avatar Jul 02 '13 16:07 taxilian

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" [email protected] 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.

tebriel avatar Jul 02 '13 16:07 tebriel

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.

taxilian avatar Jul 02 '13 17:07 taxilian

Lemme know how that goes.

tebriel avatar Jul 02 '13 17:07 tebriel

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
    _finishCallback.bind(this)();

    // add your cleanup code here...
};

sobotklp avatar Aug 21 '13 17:08 sobotklp

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

ashleyemery avatar Oct 01 '13 08:10 ashleyemery

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: https://github.com/pivotal/jasmine/pull/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 avatar Nov 21 '13 00:11 tdumitrescu

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.

prantlf avatar Jan 26 '14 15:01 prantlf

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 avatar Feb 27 '14 07:02 armw4

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

Related: https://github.com/mhevery/jasmine-node/issues/366

qfox avatar Oct 18 '16 19:10 qfox

how to run .sql file for Postgres SQL in jasmine when before running spec. I don't get any solutions for this. can I add .sql file code in jasmine.json file?

sanoapoorva avatar May 13 '20 12:05 sanoapoorva