Callbacks in Laika tests go untriggered

Meteor.collection.insert() allows for the use of a callback as one of its arguments. To demonstrate, you can start a new Meteor project and execute the following code in the browser's console.

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

However, when I try to include this code in a Laika test, the callback parameter does not seem to be executed. Below is my testing 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();
                })
        })
    })
})

Does anyone know why the callback function isn't being triggered in this Laika test? This issue appears to extend beyond just Meteor.collection.insert().

(I am using Ubuntu 13.04, Meteor 0.7.0.1, Laika 0.3.1, PhantomJS 1.9.2-6)

Answer №1

The issue arises when you attempt to execute done(); within your insert callback, even though it is not defined in that specific function scope. The correct approach is to monitor the insertion into my_collection and trigger a signal that can be intercepted by either the client or server (in this instance, the client). Also, remember not to initialize your collection within your test; this should be handled in your production code.

Consider implementing the following solution 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();
        });
    });
})

Refer to https://github.com/arunoda/hello-laika for fundamental testing examples.

Answer №2

Greetings, Mr. jonS90! Should you decide to execute Laika while utilizing the --verbose option, it would come to your attention that an exception is discreetly raised:

[client log] An error occurred when attempting to deliver the result of calling '/myCollection/insert': ReferenceError: Unable to locate variable: done

The issue lies in the fact that you do not have access to done() within that particular scope. Here's a revised version of your code:

test('inserting into collection', function(done, server, client) {
    client.eval(function() {
        my_collection = new Meteor.Collection("myCollection");

        finishedInsertion = function () {
            console.log("finished insertion");
            emit('done')
        }
        my_collection.insert(
            {some: "object"},
            finishedInsertion)
    })
    client.once('done', function() {
        done();
    })
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Rxjs to reset an observable with combineLatest

My scenario involves 4 different observables being combined using "combineLatest". I am looking for a way to reset the value of observable 2 if observables 1, 3, or 4 emit a value. Is this possible? Thank you! Mat-table sort change event (Sort class) Mat- ...

Custom virtual properties can be set in Mongoose by utilizing the return value in a callback function

I've been searching all over for a solution to my issue, but I can't seem to find the right answer. I'm currently using MongooseJS as my ODM and I'm attempting to create virtual getters that can retrieve, process, and display informatio ...

Tips for creating JSON using AngularJs to store tabular information

I successfully created an HTML page using AngularJS. <form name="target" ng-submit="createAllKeywordsJSON(codeMasterList)"><!-- createAllKeywordsJSON() --> <input type="submit" value="Save" class="myButton" name="submit" style="margin: ...

Fs claims that a file does not exist, however, it actually does

Currently, I am facing an issue with a file that contains the following code: const cmds = JSON.parse(fs.readFileSync('./cmds.json')); The file where this code is running is located in the same folder as `cmds.json`, but for some reason, it cann ...

Having trouble showing the material-ui icon on my navigation menu

How can I use Material-UI icons like <AddOutlinedIcon /> in my nav menu without displaying the actual code before the menu name? Do I need to escape the icon code somehow to make it appear correctly? The intended result is to have a + icon displaye ...

Unable to produce scrolling animation using JavaScript

I'm trying to implement a feature on my website where the page scrolls with a sliding effect when the user presses the "Scroll" button. However, I've encountered issues as it doesn't seem to work despite adding the necessary tags to my HTML ...

Ensure that the footer remains at the bottom of the page without being fixed to the bottom

I am currently working on configuring the footer placement on pages of my website that contain the main body content class ".interior-health-main". My goal is to have a sticky footer positioned at the very bottom of these pages in order to eliminate any wh ...

What is the best way to incorporate AngularJS data into JavaScript for use in Google Chart?

How can I leverage my AngularJS scope data in a Google Chart or JavaScript? Below is my AngularJS file: angular.module('reports').controller('ReportInfoCtrl', ['$scope', 'reports', '$rootScope','$loca ...

What is the best way to access a custom object in JavaScript that was created in a different function?

Currently working with JavaScript and jQuery technology. In one of my functions that runs on document ready, I am creating objects with different attributes. While I can easily access these object attributes within the same function, I'm facing diff ...

When trying to implement appDir and withPWA in next.config.js, an error has been encountered

My next.config.js is set up with next-pwa and an experimental app feature included. const withPWA = require('next-pwa'); module.exports = withPWA({ pwa: { dest: 'public', disable: process.env.NODE_ENV === 'development&ap ...

Waiting for a function to finish within a nested function in JavaScript

I'm facing a simple issue that I'm struggling to solve. I have two functions and an object in JavaScript (Node.js) structured like this: var auxmap = new Map(); function one() { for(var i...) { //initialize the map and do something tw ...

How to prevent text from overflowing outside the Material UI Container/Box?

One issue I'm facing is with an Alert that displays long strings on my website. Here's the code snippet in question: <Container maxWidth="md"> <Box sx={{ mt: 3, border:1}}> <Box> {hasSubmitted ? ...

Issues with loading Angular 9 application on Internet Explorer 11

Having trouble with my app not loading in IE 11 after adding ngx-treeview. Encountering the error (SCRIPT1002: Syntax error), Script Error Error point in vendor.js Unsure how to resolve this issue. Works fine in chrome and firefox, but in IE11 all I se ...

terminate the express middleware and return a custom HTTP status code

Is it possible to use custom middleware to return a 404 or 401 error to the user and prevent other handlers from running? I tried implementing the following code: function SomeMiddleware(req, res, next) { if(user.notRealOrSomething) { throw new Htt ...

Ramjet: Unveiling the Magic of Making Elements Appear and Disappear

Currently, I am attempting to implement the code for ramjet from . However, I am facing an issue where element a does not disappear when transitioning into b. Additionally, I am encountering an error message "--Uncaught TypeError: Cannot read property &apo ...

What is the best method for storing pug-formatted data in a database?

I am currently developing a JavaScript application where I utilize pug for templates and diskdb for storing data. Within my content, I have implemented pug syntax which may appear in the following format: p some content here p some more content here p: # ...

What is the best way to restrict event handling to only occur once every X seconds using jQuery or JavaScript?

Is there a way to limit handling the event of a rapidly-firing keypress to once per X seconds using jQuery or vanilla JavaScript? Check out this jsfiddle that demonstrates rapid keypress firing without any limiting on the handling: View here ...

Adjust the pagination length within the jQuery DataTables plug-in

I am looking to customize the pagination length in DataTables plug-in for jQuery. Specifically, I want to calculate the number of pages on the server side and display the correct amount of buttons on the client side. Can anyone provide guidance on how to ...

Leveraging Json data in Angular components through parsing

I am currently developing an angular application where I need to retrieve and process data from JSON in two different steps. To start, I have a JSON structure that is alphabetically sorted as follows: { "1": "Andy", "2": &qu ...

The ThemeProvider does not automatically provide theme injections

After creating a theme using the createTheme method from @mui/material/styles, I attempted to apply this theme using ThemeProvider from the same package. This snippet showcases the dark theme that was created: export const darkTheme = createTheme({ pale ...