Loopback Models Mocha Unit Testing

2,030 views
Skip to first unread message

Zurich

unread,
Jun 22, 2016, 9:10:50 AM6/22/16
to LoopbackJS

Hi All,

We are implementing Mocha (Chai) Unit testing for loopback models i am new to this technology. 

Please share few sample on loopback models how to testing us done using mocha

- Zurish

Akram Shehadi

unread,
Jun 22, 2016, 2:38:23 PM6/22/16
to LoopbackJS
Hi Zurich,


These two StackOverflow questions helped me setup my Mocha tests:


https://stackoverflow.com/questions/24153261/joining-tests-from-multiple-files-with-mocha-js

In particular, since I do not want to restart the server each time, I am using the following setup:

in ./test/top.js:

var app;
var supertest;

// Uncomment if you want to include common exports.options or similar
//var common = require("./common");


function importTest(name, path) {
    describe
(name, function () {
       
require(path);
   
});
};


describe
("My Test Suite", function () {
    before
(function () {
       
//console.log("running something before each test");
        app  
= require('../server/server');
        supertest
= require('supertest');
       
global.api = supertest(app);
   
});

    beforeEach
(function () {
       
//console.log("running something before each test");
   
});

    importTest
("User", './User/test');
    importTest
("CutomerAccount", './CustomerAccount/test');

    after
(function () {
        console
.log("after all tests");
   
});
});

And then for example to test the User model we have the ./test/User/test.js file:

describe('/Users test suite', function () {

    describe
('login/logout for Test Admin user', function () {
       
var token;
       
var verificationToken;
       
var demoUserId;
       
var redirectLink;

        it
('should NOT be able to create a user (without access_token)', function(done) {
            api
.post('/api/Users')
               
.send({email:"new...@example.com", password: "mypassword"})
               
.expect(401, done);
       
});

        it
('should login as Test Admin user', function(done) {
            api
.post('/api/Users/login')
               
.send({email:"test_...@example.com", password: "myadminpassword"})
               
.expect(200)
               
.end(function(err, res) {
                   
if (err) return done(err);
                    token
= res.body.id;
                   
done();
               
});
       
});

        it
('should be able to create a Demo user', function(done) {
           
            api
.post('/api/Users')
               
.query({access_token: token})
               
.send({email:"de...@example.com", password: "mydemopassword"})
               
.expect(200)
               
.end(function(err, res){
                   
if (err) return done(err);
                    verificationToken
= res.body.verificationToken;
                    demoUserId
= res.body.id;
                    redirectLink
= 'http://0.0.0.0/verified?user_id='+demoUserId;
                   
done();
               
});
       
});

        it
('should logout from Test Admin user', function(done) {
            api
.post('/api/Users/logout')
               
.query({access_token: token})
               
.expect(204, done);
       
});

        it
('should be able to verify the Demo user', function(done) {
           
            api
.get('/api/Users/confirm')
               
.query({uid: demoUserId, redirect: redirectLink, token: verificationToken})
               
.expect(302, done);
       
});
       
   
});

    describe
('login/logout for regular User', function (){
       
...
   
});

    describe
('restrictions for non-Admin users', function () {
       
...
   
});

});


So as you can see, we are creating a file for each model we want to test, and inside that we define a test suite with several sub-suites that test different things.

The reason it's setup like this is because we can enable or disable certain tests just by commenting in top.js, without affecting anything else.

However, some people prefer to have self-contained tests without common functionality like in this structure, where we are starting the server only once, at the beginning, instead of starting and then stopping inside each suite.

Not sure in your case what is preferred, but you can easily adapt it.


Hope this helps,
Akram

SYED HANIF

unread,
Jun 23, 2016, 2:58:55 AM6/23/16
to loopb...@googlegroups.com
Hello Akram,

Thank you so much for helping.

My question is maintain top.js is common js file which test all models available in the ./common/models/ - folders that is correct.

Where do i need to place the top.js files it should be in the root directory ?

ImportTest function is not working.. my folder structure is blow

MyRootApplication /common 
MyRootApplication/common/models -- all the loopback models creation is stored in these folder
MyRooltApplication / Server /Test/ -- all mocha test js files is available in the following location
E.g Folder structure will be like these below
/server/test/user/user.spec.js
/server/test/authentication.spec.js

How do i need to map the top.js file to execute all the mocha test js files available in the folder location /server/test

Appreciate your direction on these

-Zurich

E.g /server/test/

--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/3XrkUA-jI-k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/5ecce0b3-da09-4138-a294-8956eb2dd62d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Akram Shehadi

unread,
Jun 23, 2016, 11:11:28 AM6/23/16
to loopb...@googlegroups.com
Be sure to use the correct capitalization, but basically my structure is as follows:


MyApplication/                                                  <----- root folder
MyApplication/server                                         <----- server files
MyApplication/test                                            <------ test folder
MyApplication/test/top.js                                   <--------- top test file
MyApplication/test/common.js                           <--------- common exports
MyApplication/test/User/test.js                          <--------- tests for User model
MyApplication/test/OtherModel/test.js                 <--------- tests for OtherModel model
MyApplication/test/YetAnotherModel/test.js         <--------- tests for YetAnotherModel model


And so on. You can also put everything under MyApplication/test/test1.js, test2.js, etc, but I find it easier to enable/disable tests and to overall understand better what exactly I am testing.

Basically all tests should be under MyApplication/test folder.

PS. Note that "test" folder is at the same level as "server" folder, NOT inside.
/serv

SYED HANIF

unread,
Jun 24, 2016, 2:42:09 PM6/24/16
to loopb...@googlegroups.com

Thanks Akram understood.

As per your direction i have revised my loopback folder structure all the mocha test script files saved under test folder.

I am facing another issue on top.js, please correct me if my  understanding is wrong.

Top.js will be top level file placed under test folder, and top.js will call other mocha test script files through importTest method. but currently importTest is not working for me. 

When i pass the parameter name, path it not pickup the mocha test scripts.

Additionally i am using jasmie test script for client side testing still mocha unit testing is required since mocha has cross-browser support test both client & server side testing. 

Can you please clarify more details did mocha still require or will be useful for application like loopback running under nodeJS platform

-Zurich


On Thu, Jun 23, 2016 at 8:41 PM, Akram Shehadi <akram....@gmail.com> wrote:
Be sure to use the correct capitalization, but basically my structure is as follows:


MyApplication/                                                  <----- root folder
MyApplication/server                                         <----- server files
MyApplication/test                                            <------ test folder
MyApplication/test/top.js                                   <--------- top test file
MyApplication/test/common.js                           <--------- common exports
MyApplication/test/User/test.js                          <--------- tests for User model
MyApplication/test/OtherModel/test.js                 <--------- tests for OtherModel model
MyApplication/test/YetAnotherModel/test.js         <--------- tests for YetAnotherModel model


And so on. You can also put everything under MyApplication/test/test1.js, test2.js, etc, but I find it easier to enable/disable tests and to overall understand better what exactly I am testing.

Basically all tests should be under MyApplication/test folder.

Akram Shehadi

unread,
Jun 24, 2016, 11:13:48 PM6/24/16
to LoopbackJS
Hi Zurich,

Well, as far as I understand it, it's not so much that top.js will call the other files, but that it will kind of "inline them". So for example, you should be able to replace the importTest() call with the actual test code and it should work.

In the end Mocha will run whatever test file is in ./test, and since it works as a set of nested "describe" blocks (with the "it" tests inside), it will run the imported ones. So in the end the imporTest() call should just "copy" the test code into top.js so it gets executed.

My suggestion is that you try to just add one describe block inside top.js and try it out. If it works then it's some issue with the import not picking up the correct path. 

Also, I am not sure if using mocha to test client-side is the same. My use case is testing only the loopback server so not sure about what's going on. I've never used jasmine so I can't comment on that.

For me mocha is very useful to test my loopback server because it makes the same HTTP requests I'll be getting from my clients, and since in my particular use case I use operation hooks and other middleware is easier for me to test the whole stack instead of doing proper unit testing (I have time constrains and such).

What exacly is the error you are getting while trying to import? could it be that the path is incorrectly capitalized or something? Also, remember that I am using supertest, and chai as well as mocha, so maybe the error is about an unmet dependency?

If you paste the error you are getting maybe I can take a look.

It's strange because I'm using a regular loopback structure and I pretty much just copy-pasted the info on the StackOverflow questions and adapted them to my particular use, but didn't really make any heavy changes.

Akram

SYED HANIF

unread,
Jun 27, 2016, 1:45:56 PM6/27/16
to loopb...@googlegroups.com
Greeting Akram,

Thank you so much for your reply.

I am have installed the following NPM Packages in working folders

1. npm install -g mocha chai --save-dev
2. npm install supertest
3. npm install

As per your environment same i also use mocha for loopback server to check HTTP request. All the *Spec.JS files stored in root folder called Test and also top.js is available in test folder only.

I am not getting any exception and also none of the *Spec.js is getting executed. I believe Loopback server will check only the models not the controller because all of business logic & api calling written directly in HRMS.Service.js file that is the problem - Please provide your technical comments if you want i can share my HRMS.Service.js file to you for review.

I believe to execute the mocha test script i need to remove the application logic written in HRMS.Service.js file and re-written in model.js file. we use model.json file for database scheme.

Another problem i am facing in loopback migration script, since i am using MySQL i need to migrate the update script from local database to development or production database server. I need to auto-migration process to setup in loopback. 

Please provide your valuable solution.

-Zurich

SYED HANIF

unread,
Jul 1, 2016, 3:48:52 AM7/1/16
to loopb...@googlegroups.com
Akram,

Any updates on the below request.

Mohd Iqmal Azim

unread,
Jul 3, 2018, 6:28:50 AM7/3/18
to LoopbackJS
Thanks man, this is really great, really help me out
Reply all
Reply to author
Forward
0 new messages