The first runs creates a user, and the second runs gets the user from the server. The WaitsFor is supposed to cause the second runs to wait until the first runs is done. However, when running node-jasmine, the test stops after before: 51c21c1ef463390000000008
is printed, without making the get request.
I've tried taking out the waitsFor, but that causes the get request to happen prior to the post request, which causes it to fail because it depends on a new user's userid
.
What's going on?
var request = require('request');
describe("User", function(){
var userid;
it ("creates user", function(){
runs(function(){
var username = 'test' + Math.floor((Math.random()*100000)+1);
var body = {
username: username,
email: username + "@test.com",
password: username + "@test.com"
};
var options = {
uri: 'http://localhost:3000/api/user',
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(body)
};
request(options, function(err, res, body) {
expect(res.statusCode).toEqual(200);
console.log(body);
var newUser = JSON.parse(body)[0];
userid = newUser._id;
console.log("before: "+ userid);
expect(newUser.toBeTruthy());
});
});
waitsFor(function() {
console.log(typeof userid != "undefined");
return (typeof userid != "undefined");
}, "userid is never set", 10000);
//it ("gets user", function() {
runs(function(){
expect(userid).toBeFalsy();
console.log("this: "+userid);
request.get("http://localhost:3000/api/user/" + userid, function(err, res, body){
console.log("statuscode:" + res.statusCode);
expect(res.statusCode).toEqual(2000);
expect(body).toBeTruthy();
console.log('Response: ' + body);
});
});
});
});