javascript - Export function/class from node.js module -
i have class i've defined in javascript this:
var coolclass = function() { this.prop1 = 'cool'; this.prop2 = 'neato'; } coolclass.prototype.docoolthings = function(arg1, arg2) { console.log(arg1 + ' pretty ' + this.prop1; } modules.export = coolclass;
i need able export can test in mocha using require. i'd still allow class instantiated in browser.
as of now, can load browser, instantiate , it's go. (obviously error in console not understanding keyword 'export' or 'module')
typically export multiple single functions using
exports.somefunction = function(args){};
but want export 1 function, none of methods i've added via prototype chain defined.
i've tried module.exports doesn't seem trick either. mocha spec requires file this:
var expect = require('chai').expect; var coolclass = require('../cool-class.js'); var mycoolclass; beforeeach(function() { mycoolclass = new coolclass();// looks here issue }); describe('coolclass', function() { // if instantiate class here, works. // methods added coolclass undefined });
it looks beforeeach in mocha gets tripped up. can instantiate class in actual spec, , works fine.
on mochajs, need put beforeeach
inside parent describe
, , have specific scenarios on children describe
s. else, things done in beforeeach
doesn't recognized describe
. mycoolclass treated global variable , nothing instantiated, that's why prototype functions undefined.
so it's (sorry i'm on mobile):
var mycoolclass = require('mycoolclass.js'); describe('mymodule', function () { var mycoolclass; beforeeach(function () { mycoolclass = new mycoolclass(); }); describe('my scenario 1', function () { mycoolclass.dosomethingcool('this'); //or assert }); });
you can @ documentation further details.
Comments
Post a Comment