2

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

pagid
  • 13,559
  • 11
  • 78
  • 104
  • I don't think tests are ran instantly (and they even might be async), which is why your code doesn't work. Anyway, why don't you just let the other tests fail? – jgillich Feb 26 '14 at 20:06
  • Well the first test would fail and the other tests would run into timeouts - that would basically waste time and would not provide any additional information. That's why I was hoping to introduce this dependency. – pagid Feb 26 '14 at 20:11

1 Answers1

1

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.

ossek
  • 1,648
  • 17
  • 25
  • Hi - thanks that seems to be working. My reason for doing things in such a complex is that I've a node component which talks to a (hopefully) running Jetty service. Of course I've tests which mock the external service, but in my case Jetty is lightweight enough to have a couple of tests which run with the actual service. Thanks for your answer. – pagid Mar 04 '14 at 13:43