Wednesday, February 15, 2017

How to write Jasmine asynchronous test

The Jasmine documentation at https://jasmine.github.io/edge/introduction says:
 
describe("long asynchronous specs", function() {
    beforeEach(function(done) {
      done();
    }, 1000);

    it("takes a long time", function(done) {
      setTimeout(function() {
        done();
      }, 9000);
    }, 10000);

    afterEach(function(done) {
      done();
    }, 1000);
  });
 
I know I can have a callback function in one file (called callbacj3.js):

 var fs = require('fs');

module.exports = {
   pasty: function(url, fn) {

      fs.readFile( __dirname + url, function (err, data) {
           if (err) {
           throw err;
         }
       fn(data.toString());

      });
   }
}
 
 
And a second file referencing it (called  callbacj3-ref2.js):
var acallnack = require('./callbacj3');
var url = '/namespaces.txt';

acallnack.pasty(url, function(duck) {
  console.log(duck);
})
And even a third file referencing it (called var acallnack = require('./callbacj3');
var url = '/namespaces.txt';

function cals(url, fn) {
  acallnack.pasty(url, function(duck) {
    fn(duck);
   })
 }

cals(url, function(goody) {
  console.log(goody);
});

Using the Jasmine syntax above, how do a use callbacj3-ref2 and callbacj3-ref to construct
a test?
 
https://discuss.atom.io/t/correct-way-to-test-asynchronous-ipc-calls-with-jasmine/20038/3 
 
SeeAlso:
 

Testing Asynchronous Code - JavaScript Testing - Udacity: https://www.youtube.com/watch?v=C5NGKzxe5vs

 

Correcting our Asynchronous Test - JavaScript Testing - Udacity:
https://www.youtube.com/watch?v=C5NGKzxe5vs

 

No comments:

Post a Comment