dimanche 17 avril 2016

How to use require in karma/jasmine test enviroment?

I am not looking for requireJS

I've build a module and want to require it in my test suite. How to tell karma to rquire the dependencies?

index.js

/* global window, document, define, jQuery, setInterval, clearInterval */
(function(factory) {
    'use strict';
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else if (typeof exports !== 'undefined') {
        module.exports = factory(require('jquery'));
    } else {
        factory(jQuery);
    }
}(function($) {
    'use strict';

    var Warp = window.Warp || {};

    Warp = (function() {

        var instanceUid = 0;

        console.log('Loaded slides');

    });

    $.fn.warp = function() {
        var that = this,
            opt = arguments[0],
            args = Array.prototype.slice.call(arguments, 1),
            l = that.length,
            i,
            ret;

        for (i = 0; i < l; i++) {
            if (typeof opt == 'object' || typeof opt == 'undefined') {
                that[i].warp = new Warp(that[i], opt);
            } else {
                ret = that[i].warp[opt].apply(that[i].warp, args);
                if (typeof ret != 'undefined') return ret;
            }
        }
        return that;
    }
}));

karma.conf.js

// Karma configuration
// Generated on Sun Apr 17 2016 14:12:55 GMT+0200 (CEST)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: http://ift.tt/1ft83uu
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'index.js',
      'test/*.spec.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: http://ift.tt/1gyw6MG
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: http://ift.tt/1ft83KQ
    reporters: ['spec', 'clear-screen'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: http://ift.tt/1ft83KU
    // browsers: ['Chrome', 'PhantomJS'],
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity,

  })
}

Before I was testing manually without karma and had to require my library to test the code correctly. So the previouse "testing suite" was:

example.js

// budo example.js --live --host localhost

var warp = require('./index');
var $ = require('jquery');

// Creating slides container
var slides = document.createElement('div');
// Adding attributes
slides.setAttribute('class', 'slides');
// Adding slides container to document body
document.body.appendChild(slides);
// Initiating jquery-warp-scroll on slides container
var test = $('.slides').warp({
    dots: false,
    transition: 'linear'
});

console.log(test.length);

Now I am asking: How to require my dependencies correctly with karma-jasmine testing enviroment?

Aucun commentaire:

Enregistrer un commentaire