I'm trying to unit test with Mocha but I'm kind of stuck.
I've seen this post https://community.apigee.com/articles/3964/unit-testing-javascript-code-with-mocha-sinon-and.html but can't get my tests to run without breaking my Api proxy.
I'd like to test this file: myapi/src/gateway/myapi/apiproxy/resources/jsc/util/myUtil.js
myutil.js serves a util for jsc/mypolicy.js it just does some data manipulation.
Now, in order for me to use mocha, I need to expose myUtil.js by adding
modules.exports = myUtil;
However, as soon as I do that, the Api proxy tracer spits out this error:
ReferenceError: "module" is not defined
So my question is, how am I able to reference myUtil from a mocha test file, like so:
myUtil = require(path/to/myUtil); resp = myUtil.manipulate(oldResponse); expect(resp.nextPage).to.equal("example.com");
Solved! Go to Solution.
Hi @Vicente Lee
You can use the rewire module for that. Here is an example:
Assume your myUtils.js is something like this:
// myUtils.js function add(a, b) { return a + b; }
You can write the following test using rewire:
var expect = require('expect.js'); var rewire = require('rewire'); var app = rewire('./myUtil.js'); describe('feature: add', function() { it('should add two numbers', function() { var add = app.__get__('add'); expect(add(1,1)).to.equal(2); }); });