I'm trying to run tests on my npm module. The code below will work if I comment out either of the 2 it
blocks, but times out if I leave them both in as below. How can wait for "ready" before running my tests (I'd like to add more but they too need to await "ready")?
describe("GA analytics", function() {
var report = new Report(private_key, SERVICE_EMAIL, 1);
it("should connect and emit <ready>", function(done) {
report.on('ready', function() {
console.log("test.js: Token: ", report.token);
expect(report.token).toBeDefined();
done();
});
});
it("should get data correctly", function(done) {
report.on('ready', function() {
report.get(query, function(err, data) {
if (err) throw err
expect(data.rows).toEqual([ [ '5140' ] ]);
done();
});
});
});
});
I guess it is happening because you create a new instance of the Report
only once per test file, therefore the ready
event only fires once and only the first it
block in the test will catch it and process. The rest of the it
blocks won't receive any more ready
events, so they silently wait for it until Jasmine times out. The solution will be to create a new Report
instance before each it
block, which can be easily done with the help of Jasmine beforeEach
:
describe("GA analytics", function() {
var report;
beforeEach(function () {
report = new Report();
});
// ....
});
See the working example here on Plunker (open a "script.js" file)