lundi 3 avril 2017

RequireJS and Mocha

I have a web app using RequireJS. Here is my js/main.js file:

require.config({
    baseUrl: 'js/',
    paths: {
        jquery: 'libs/jquery/jquery',
        lodash: 'libs/lodash/lodash',
        backbone: 'libs/backbone/backbone',
        // [other dependencies...]
    }
});

require(['views/AppView'], function (AppView) {
    var app_view = new AppView;
});

here is my js/views/AppView.js file:

define([
    'jquery',
    'underscore',
    'backbone',
    'joint',
    'views/ProjectView',
    'models/Command',
    'views/detailsview',
    'views/newcellview'
], function ($, _, Backbone, joint, ProjectView, Command, DetailsView, NewCellView) {
    var app_view = {stub: 'stub'};
    return app_view;
});

and finally here is my AppViewTest.js file which I run with mocha js/test/AppViewTest.js:

var assert = require('assert');
var requirejs = require('requirejs');

describe('AppView', function() {
    it('should be [...]', function() {
        var app_view;
        requirejs(['../views/AppView.js'], function (AppView) {
            app_view = new AppView;
        });
        assert.equal(app_view, ...);
    });
});

But, apart from the "..." stub I've put for reading purposes, running the test reveals that the app_view variable doesn't get initialized at all, thus remaining undefined...

I have tried this solution but Mocha says it doesn't know any setup() function.

Aucun commentaire:

Enregistrer un commentaire