loopbackjswallaby.js

Using Wallaby with LoopbackJS


I'm trying to use Wallaby with LoopbackJS and last couple of hours brought only failures. Maybe someone here already figured out how to do it. Here is my wallaby config that is closest to "it's working"

 module.exports = function () {
      return {
        files: [
          'server/**/*.js',
          'common/**/*.js',
          'test/global.js'
        ],
        tests: [
          'test/models/*.js',
          'test/services/*.js'
        ],
        workers: {
          recycle: true //doesn't matter whats set here
        },
        bootstrap: function (wallaby) {
          // try number 1
          if (global.app) return;
          var path = require('path');
          var loopback = require('loopback');
          var boot = require('loopback-boot');

          wallaby.delayStart();
          global.app = loopback();
          // instead of __dirname I was trying also localProjectDir and projectCacheDir
          boot(global.app, path.join(__dirname, 'server'), function () {
            wallaby.start();
          });
          // try number 2
          var path = require('path');
          if (global.app) return;
          global.app = require(path.join(wallaby.localProjectDir, 'server/server.js'));
        },
        env: {
          type: 'node',
          params: {
            env: 'NODE_ENV=test'
          }
        }
      };
    };

Try number 1 starts but the app seems to be not configured. app.get('some-config') as well as app.models.myModel is always undefined. Try number 2 seems to be a bit better because it runs some tests but it throws the same errors as first one.


Solution

  • Changing your files patterns from **/*.js to **/*.* should help, because there are also json files and they do participate in models and config initialisation, so wallaby needs them as well. You also need to use process.cwd() or wallaby.projectCacheDir instead of wallaby.localProjectDir so that wallaby can do the code coverage for you.

    module.exports = function () {
        return {
            files: [
              'server/**/*.*',
              'common/**/*.*',
              'test/global.js'
            ],
            tests: [
              'test/models/*.js',
              'test/services/*.js'
            ],
    
            bootstrap: function (wallaby) {
              if (global.app) return;
              var path = require('path');
              global.app = require(path.join(process.cwd(), 'server/server.js'));
            },
    
            env: {
              type: 'node',
              params: {
                env: 'NODE_ENV=test'
              }
            }
          };
    };