I am trying to write a Mocha test case. Have installed Chai & Sinon.
However when i try to run the test i get this error
Exception during run: TypeError: context.setVariable is not a function
My source js file is JS-GenerateUID.js
var transactionId = "";
var traceId = "";
transactionId = generateUUID();
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
});
context.setVariable("traceId", uuid);
context.setVariable("transactionId", uuid);
return uuid;
}
export { generateUUID };
And my Mocha test case is Generate-UUID.test.js
import { assert } from 'chai';
import {generateUUID} from "../apiproxy/resources/jsc/JS-GenerateUID.js";
describe('generateUUID', () => {
it('should return a string with a valid UUID format', () => {
const uuid = generateUUID();
assert.match(uuid, /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
});
it('should set traceId and transactionId variables with the generated UUID', () => {
// Mock context object
const context = {
setVariable(key, value) {
this[key] = value;
}
};
// Override context object for the test
global.context = context;
const uuid = generateUUID();
assert.strictEqual(context.traceId, uuid);
assert.strictEqual(context.transactionId, uuid);
});
// Add more test cases as needed
});
Whats wrong with the code? My guess is the context variable of apigee probably needs to be mocked or something.
But dont know, any answers would be helpful.
Thanks & Best regards,
Amitt Nerrkar