Skip to content

hatch-is/loopback-testing

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#loopback-testing

Utilities for testing LoopBack apps

overview

The following helpers are designed to generate [mocha] tests against LoopBack apps.

install

  1. npm install loopback-testing --save-dev
  2. Assuming you started with a clean template/project generated by slc loopback
  3. If you have mocha installed as a global npm module that's great! Simply update <your_project>/package.json with:
```
{
  ...
  "scripts": {
    ...
    "test": "mocha"
  }
}
```
  1. Otherwise, you can utilize the mocha library within the loopback-testing testing module:
```
{
  ...
  "scripts": {
    ...
    "test": "./node_modules/loopback-testing/node_modules/.bin/mocha"
  }
}
```
  1. Run npm test to execute any tests under the test directory.

basic usage

Below is a simple LoopBack app.

var loopback = require('loopback');
var app = loopback();
var Product = app.model('product');
Product.attachTo(loopback.memory());

Use the loopback-testing module to generate mocha tests.

var lt = require('loopback-testing');
var assert = require('assert');
var app = require('../server/server.js'); //path to app.js or server.js

describe('/products', function() {
  lt.beforeEach.withApp(app);
  lt.describe.whenCalledRemotely('GET', '/products', function() {
    lt.it.shouldBeAllowed();
    it('should have statusCode 200', function() {
      assert.equal(this.res.statusCode, 200);
    });

    lt.beforeEach.givenModel('product');
    it('should respond with an array of products', function() {
      assert(Array.isArray(this.res.body));
    });
  });
});

call remote only once

By default whenCalledRemotely will be run before each it(). This can be changed to run only once by adding a $once as a param to the callback function (i.e. lt.describe.whenCalledRemotely('GET', '/', function($once) {..}))

var lt = require('loopback-testing');
var assert = require('assert');
var app = require('../server/server.js'); //path to app.js or server.js

describe('/products', function() {
  lt.beforeEach.withApp(app);
  lt.describe.whenCalledRemotely('POST', '/products', {
      name: 'product-1'
    }, function($once) {
      
      var id;

      it('should have statusCode 200', function() {
        id = this.res.body.id;
        assert.equal(this.res.statusCode, 200);
      });

      it('should have only been called once', function() {
        assert.equal(this.res.body.id, id);
      });
    });
});

building test data

Use TestDataBuilder to build many Model instances in one async call. Specify only properties relevant to your test, the builder will pre-fill remaining required properties with sensible defaults.

var TestDataBuilder = require('loopback-testing').TestDataBuilder;
var ref = TestDataBuilder.ref;

// The context object to hold the created models.
// You can use `this` in mocha test instead.
var context = {};

var ref = TestDataBuilder.ref;
new TestDataBuilder()
  .define('application', Application, {
    pushSettings: { stub: { } }
  })
  .define('device', Device, {
     // use the value of application's id
     // the value is resolved at build time
     appId: ref('application.id'),
     deviceType: 'android'
  })
  .define('notification', Notification)
  .buildTo(context, function(err) {
    // test models are available as
    //   context.application
    //   context.device
    //   context.notification
  });

About

Utilities for testing LoopBack apps

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%