Search code examples
javascriptjasminejasmine-nodefrisby.js

Get Frisby.js tests to run synchronously


I have these API calls in my test that need to run first so I can store the response in a variable to use later. But it looks like my tests are running asynchronously so the second test finishes before the variable gets populated. How can I make the tests run synchronously?

I've heard that one way is to use before and passing the done callback. But I'm not sure how to do that with jasmine-node.

Example of test:

var dataID = '';
frisby.create('Get ID')
  .get(url)
  .expectStatus(200)
  .afterJSON(function(json) {
     dataID = json.id;
  })
.toss();

frisby.create('Get data with ID')
  .get(url, id)
  .expectStatus(200)
  .expectJSON({"id": dataID})
.toss();

EDIT:

So I tried doing my test like this and the done() callback doesn't seem to get called. (The test times out)

describe('API TEST', function() {
  beforeEach(function(done) {
    frisby.create('Get ID')
      .get(url)
      .expectStatus(200)
      .afterJSON(function(json) {
        dataID = json.id;
        done();  //?
      })
      .toss()
  });
  it('should work', function() {
    console.log('TEST');
  }); //"timed out after 5000 msec waiting for spec to complete"
});

Solution

  • What I ended up doing was using the async library and doing .timeout(60000) on the actual frisby test like so:

    async.series([
      function(cb) {
        frisby.create('Get ID')
          .get(url)
          .expectStatus(200)
          .afterJSON(function(json) {
            dataID = json.id;
            cb();
          })
          .toss();
      },
      function() {
         //other tests using id
      }
    ]);