I'm trying to wrap my head around what's happening when jasmine-node runs server tests on my Node Express app. This example uses the beforeEach functions to create a user, log the user in, run some tests, then delete the user with an afterEach function. I'm saving the response from the beforeEach functions as objects, so they can be passed into the tests. All this works fine, but when I try to use the response object to access the user's auth token, the test fail. Instead, the user's auth token has to be stored as a primitive type.
describe('POST /users', function () {
// declare the variables, the user token has to be a primitive type.
var userToken;
var newUser = {};
var logUser = {};
var createOptions = {
url: 'http://localhost:3000/users',
method: 'POST',
json: {
email: "test1@email.com",
password: "testpass"
}
};
var loginOptions = {
url: 'http://localhost:3000/users/login',
method: 'POST',
json: {
email: "test1@email.com",
password: "testpass"
}
};
beforeEach(function (done) {
function createCallback(error, response, body) {
if ( error ) {
return console.log(error);
} else {
newUser.res = response;
newUser.view = body;
}
done();
}
function loginCallback(error, response, body) {
if ( error ) {
return console.log(error);
} else {
// store the responses as an object or a string
logUser.res = response;
logUser.view = body;
userToken = response.headers.auth;
}
done();
}
request(createOptions, createCallback);
request(loginOptions, loginCallback);
});
afterEach(function (done) {
console.log(userToken);
request({
url: 'http://localhost:3000/user',
method: 'DELETE',
headers: {
// pass the user token into the header, so the entry can be deleted.
// won't work with an object: newUser.res.headers.auth
Auth: userToken
}
});
done();
});
it('creates a new user with valid credentials', function (done) {
expect(newUser.res.statusCode).toBe(200);
done();
});
it('logs in a user with valid credentials', function (done) {
expect(logUser.res.statusCode).toBe(200);
done();
});
});
When I run the tests, and console.log the user token, I can see it initially logs undefined, then logs the token. I assume this is how jasmine-node forces the code block to run synchronously; it is probably checking for undefined before executing, then timing out if it continues to find undefined:
> jasmine-node spec
// console.log(userAuth) is only called once, but it looks like it is checking at least twice.
..undefined
.eyJ0eXAiOiJKV-etc-etc
..
Finished in 0.481 seconds
The test will fail if I try to use the variable stored in an object. Here is the failing code and output:
afterEach(function (done) {
console.log(logUser.res.headers.auth);
request({
url: 'http://localhost:3000/user',
method: 'DELETE',
headers: {
Auth: logUser.res.headers.auth
}
});
done();
});
> jasmine-node spec
// note the first character is an F for the failing test
..FeyJ0eXAiOiJKV1QiLCJ-etc-etc
..
Failures:
1) POST /users creates a new user with valid credentials
Message:
TypeError: Cannot read property 'headers' of undefined
at Timer.listOnTimeout (timers.js:92:15)
Finished in 0.608 seconds
So it looks like it is logging a valid user token, but still evaluating as undefined in the afterEach code block.
Aucun commentaire:
Enregistrer un commentaire