简体   繁体   中英

Testing modules packaged with browserify?

I'm writing a test for a module that is managed with browserify. This is the module I want to test (specifically I want to test calculateData ):

require('mapbox.js');
var utils = require('./src/chart_utils');

var dashboard = {
    calculateData: function(data) {
        // functions to test
    }
};

I've started to write a test like this:

var expect = require('chai').expect;
var Dashboard = require('../dashboard');

// describe('Dashboard', function () {
//     describe('#convertData', function () {
//         it('should calculate the x and y value', function () {
//             var combinedData = [];
//             var data = Dashboard.convertData(combinedData);
//             expect(data.length).to.equal(2); // etc
//         });
//     });
// });

But even the first two lines give me an error:

node_modules/mapbox.js/node_modules/leaflet/dist/leaflet-src.js:513
    ua = navigator.userAgent.toLowerCase(),
         ^
ReferenceError: navigator is not defined

It looks as though it is testing all the included module files, and finding an error.

Is there a way I can get around this, and just test the functions in dashboard ?

Dis you specify in browserify configuration ( "nodejs" compatibility mode) ? this behavior can come from a client side polyfill that you don't need. Also, browserfify will follow all "require" directive, if the function you want to test don't require a module, don't require it.

You need Dashboard to test, and Dashboard needs mapbox.js, so there's no point in testing without mapbox.js. You're getting the error because navigator doesn't exist in node and it can't execute the code -- it breaks when navigator is referenced. I'd recommend you setup Karma for test running. It can load your source and test files in a browser (or multiple browsers) and run your tests there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM