📜 ⬆️ ⬇️

Test the MeteorJS application with Laika

Hello! This small post is devoted to Laika's MeteorJS application testing system from Arunoda Susiripala . Its features are quite interesting:




Installation


1. Install using npm:
sudo npm install -g laika 

2. Download and install PhantomJS .
3. Run mongodb as follows:
 $ mongod --smallfiles --noprealloc --nojournal 

')

Simplest Testing App


Create a new application.
 $ meteor create meteor-laika 

Create the collections directory and the posts.js file
 // collections/posts.js Posts = new Meteor.Collection('posts'); 

Create a tests directory with posts.js in it
 //tests/posts.js var assert = require('assert'); suite('Posts', function() { test('in the server', function(done, server) { server.eval(function() { Posts.insert({title: 'hello title'}); var docs = Posts.find().fetch(); emit('docs', docs); }); server.once('docs', function(docs) { assert.equal(docs.length, 1); done(); }); }); }); 

Here we created a test suite with the name "Posts", in which there is one test with the name "in the server". Accordingly, in this test, we check whether an entry is added to the collection. To do this, we wrote the following:
  server.eval(function() { Posts.insert({title: 'hello title'}); //   var docs = Posts.find().fetch(); //    emit('docs', docs); //     }); 

When we call the emit function, the once method is called with the appropriate name. Therefore, after these operations, we get here:
  server.once('docs', function(docs) { //   assert.equal(docs.length, 1); //     ,    done(); //   }); 

The test is ready. To test the application, we write
 $ laika 

The following will be displayed:

Testing on the client and server


We also insert this test inside our test suite:
 test('using both client and the server', function(done, server, client) { server.eval(function() { Posts.find().observe({ added: addedNewPost //    ,   ,   addedNewPost }); function addedNewPost(post) { emit('post', post); //   "post"   post (    ) } }).once('post', function(post) { //   "post" assert.equal(post.title, 'hello title'); //     ,     done(); //   }); client.eval(function() { Posts.insert({title: 'hello title'}); //     }); }); 

The result will be the following:

Test with two clients


Add the following code inside our test suite:
 test('using two client', function(done, server, c1, c2) { c1.eval(function() { Posts.find().observe({ added: addedNewPost //         }); function addedNewPost(post) { emit('post', post); //      ,      "post" } emit('done'); //  "done" }).once('post', function(post) { assert.equal(post.title, 'from c2'); //    ,  ,   done(); }).once('done', function() { c2.eval(insertPost); //      insertPost }); function insertPost() { Posts.insert({title: 'from c2'}); //     (  ,   ),   } }); 

The result will be:


Also, I want to note that if you use iron-router in your application, then, before testing, create at least one view, without it all tests will fall.

There are quite a lot of examples on the site, you can see them here . The record turned out to be small, but, I hope, useful for those who are beginning to understand this interesting framework. It is a free translation of this page with some additions. Also, it would be interesting to hear how you are testing your applications on MeteorJS at the moment and whether you are interested in laika.

Source: https://habr.com/ru/post/213171/


All Articles