javascriptmeteorlaika

Callbacks in Laika tests are not called


Meteor.collection.insert() accepts callback as an argument. As an example, one can create a brand new Meteor project and run the following code in the browser's console.

my_collection = new Meteor.Collection("myCollection");
my_collection.insert(
    {some: "object"},
    function() {
        console.log("finished insertion");
    })

When I take this same code and put it in a Laika test, the callback argument never gets called. Here is my test code:

suite('testing Laika out', function() {
    test('inserting into collection', function(done, server, client) {
        client.eval(function() {
            my_collection = new Meteor.Collection("myCollection");
            my_collection.insert(
                {some: "object"},
                function() {
                    console.log("finished insertion");
                    done();
                })
        })
    })
})

Anyone know why the callback function isn't called in this Laika test? This seems to be an issue for more than just Meteor.collection.insert().

(I'm running Ubuntu 13.04, Meteor 0.7.0.1, Laika 0.3.1, PhantomJS 1.9.2-6)


Solution

  • The problem is that you're trying to call done(); inside of your insert callback, when it doesn't exist in that function scope. You actually need to listen for the insertion into my_collection and emit a signal which is picked up by either the client or server (the client in your case). Also, you obviously won't be initializing your collection in your test; that should be done in your production code.

    Try this instead:

    var assert = require("assert");
    
    suite('testing Laika out', function() {
        test('inserting into collection', function(done, server, client) {
    
            client.eval(function() {
                addedNew = function(newItem) {
                    console.log("finished insertion");
                    emit("done", newItem)
                };
                my_collection = new Meteor.Collection("myCollection");
                my_collection.find().observe({
                    added: addedNew
                });
                my_collection.insert(
                    {some: "object"}
                )
            }).once("done", function(item) {
                    assert.equal(item.some, "object");
                    done();
                });
        });
    })
    

    Check out https://github.com/arunoda/hello-laika for the basic examples for testing.