I try to use jasmine-node tests to test some external API tests. But running the entire test suite does only make sense if the basic connectivity works. So this basically means that I need to hand over that information from a simple ping test to all the others.
That's what I tried but this doesn't succeed even if the first test passes:
var canConnect = false;
describe("The API client", function () {
it("can connect server", function (done) {
api.ping(function (err) {
expect(err).toBeNull();
canConnect = true;
done();
})
});
// pointless the run these if the ping didn't work
if (canConnect) describe("connected clients", function () {
it("can use the API", function (done) {
api.someOtherRequest(function(err) {
expect(err).toBeUndefined();
done();
});
})
});
})
Any suggestions? Maybe even a way to solve this smarter?
Cheers
I'm curious why you are trying to test the Api functions by calling them as a client? If you are the developer of the Api, it seems you should test the functionality of the api server-side, and then mock it for your client tests.
But, if you must do this, you can guarantee that anything in the beforeEach
will be executed before each test. Put your ping test in an outer describe
to have it run first, then in the beforeEach
of the nested describe
, check connectivity.
describe('first block',function(){
beforeEach(function(){
//do initialization
});
it('should do the pinging',function(){
//normally test your Api ping test here
expect('this test called first').toBe('this test called first');
});
describe('now that we have tested pinging',function(){
var canConnect;
var checkConnectible = function(){
////Normally do your api ping check here, but to demonstrate my point
////I've faked the return
//api.ping(function (err) {
//if (typeof(err) !== "undefined" && err !== null){
// return true;
//}
//return false
//to see the main point of this example
//change this faked return to false or true:
return false;
};
beforeEach(function(){
canConnect = checkConnectible();
});
it('should only be run if canConnect is true',function(){
if (canConnect){
//expect
}
else{
expect('').toBe('cannot run this test until connectible');
}
});
});
});
You can see Since the connectivity check is done before each of the tests, you can give different results based on the result of the check. You could also do some sort of timeout checking in checkConnectible and return true or false dependent on that.