Skip to content
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

hangs at end of tests #41

Closed
jperry opened this issue Mar 5, 2015 · 5 comments
Closed

hangs at end of tests #41

jperry opened this issue Mar 5, 2015 · 5 comments

Comments

@jperry
Copy link

jperry commented Mar 5, 2015

When running against a set of nodejs tests they pass but it never exists.

gulp.task('test-server', function () {
  return gulp.src('spec/server/**/*.js')
    .pipe(jasmine());
});

Output which then requires a ctrl-c

[23:11:11] Starting 'test-server'...
Express server listening on 9000, in development mode
....

4 specs, 0 failures
Finished in 0 seconds
[23:11:13] Finished 'test-server' after 2.12 s

Any idea?

@jbblanchet
Copy link
Contributor

I don't have a lot of details, so I'm sorry if I ask obvious questions, but are you closing your Express server at the end of the tests?

@jperry
Copy link
Author

jperry commented Mar 5, 2015

no. Is there something to pipe in as jasmine automatically handles the starting of the express server so I would have expected it to stop on it's own.

@jbblanchet
Copy link
Contributor

Neither jasmine nor gulp-jasmine knows express, so your express server is either started in your specs or by one of the module you load. Once again, I don't have a lot of details on your use case, but if I were to guess, you probably have something like this:

server.spec.js:

var server = require("../server");

server.js:

var express = require("express");
var app = express();
app.listen(9000);

Is that the case?

@jperry
Copy link
Author

jperry commented Mar 5, 2015

yes, that is the pretty similar to what we have. I wonder how jasmine-node does it then within grunt which is what we had before. Sorry for not providing enough information.

@jbblanchet
Copy link
Contributor

I haven't worked with grunt that much, so I can't help you on that, but I think the current jasmine behavior is correct. Setup and teardown are the responsibility of the tests, not the framework. I'll close this issue, but feel free to discuss it further if you think this is a required feature.

If you want a quick fix, you can listen to the end of the stream in your gulp file with: .on('end', function() { and force close node.

As an aside, a pattern that I found useful for building and testing express servers is setting the server up in a module and starting it in another. Something like that:

server.js

var express = require("express");

function Server () {
    this._app = express();

    this._app.get("/", function (req, res) {
        res.send("Hello world!");
    })
}

Server.prototype.start = function (port, otherOptions) {
    this._instance = this._app.listen(port);
};

Server.prototype.stop = function (cb) {
    this._instance.close(cb);
};

module.exports = new Server();

start.js

var server = require("./server");

server.start(process.env.port, "other options");

This way, when you import server.js in your specs, you can start and stop it manually in your tests, either in beforeAll/endAll or beforeEach/endEach depending on the atomicity of your tests.

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants