javascript - mochajs referenceerror when testing function not in test file -
i have mochajs test file , javascript code file in setup below:
/js/module/codefile.js /js/test/testfile.js
the codefile.js contains javascript functions eg:
function addnumbers(a, b){ return a+b; }
the testfile.js calls functions in codefile test them:
describe("add numbers test", function() { it("checks valid result", function() { var = 2; var b = 1; var result = addnumbers(a, b); expect(result).to.equal(3); }); });
from command line cd js folder (parent of test , module directories) run mocha , following error: referenceerror: addnumbers not defined @ context <anonymous> (test/testfile.js).
i can't see how defined how can mocha know function comming from? (nb using client side js can't use import, , havent see way specificy (in mocha or karma or js in general) functions defined in python or java). ideas on how can simple unit tests running in mocha?
i tried getting mocha run in webstorm gave after similar errors.
well, mocha command nodejs program. means can use nodejs's module system load function.
function addnumbers(a, b){ return a+b; } module.exports = addnumbers;
and in test file have
var addnumbers = require('../module/codefile.js'); describe("add numbers test", function() { it("checks valid result", function() { var = 2; var b = 1; var result = addnumbers(a, b); expect(result).toequal(3); }); });
however, said using code on front-end. in case check if module
object exists. if exists means file required mocha unit testing.
function addnumbers(a, b){ return a+b; } if (module && module.exports) { module.exports = addnumbers; }
if want rid of nasty if
's, can bundle modules using browserify. browserify helps code on front-end using nodejs's module system. code remain same.
Comments
Post a Comment