Announcements
The Google Cloud Community will be in read-only from July 16 - July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Mocha test cases for Apigee Javascript policy.

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


 

 

 

4 3 374
3 REPLIES 3

razvanbulzan
Former Googler

The func generateUUID it does not hace the context injected, you have to options to do so:

Option 1: Pass the context object as an argument (easiest)

Modify the generateUUID function to accept a context object as an argument:

function generateUUID(context) {
// ... existing code ...

context.setVariable("traceId", uuid);
context.setVariable("transactionId", uuid);
return uuid;
}



In your test, pass the mocked context object to the generateUUID function:

 

it('should set traceId and transactionId variables with the generated UUID', () => {
    const context = {
        setVariable(key, value) {
            this[key] = value;
        }
    };

    const uuid = generateUUID(context); // Pass the mocked context

    assert.strictEqual(context.traceId, uuid);
    assert.strictEqual(context.transactionId, uuid);
});


Option 2: Use Sinon to spy on the context object (if available)

If you're using Sinon for mocking, you can leverage it to spy on the global context object and verify its interactions.

Import Sinon in your test file:

 

const sinon = require('sinon');



Create a spy on the setVariable method of the global context object:

it('should set traceId and transactionId variables with the generated UUID', () => {
const context = {
setVariable: sinon.spy()
};
global.context = context;

const uuid = generateUUID();

sinon.assert.calledWith(context.setVariable, "traceId", uuid);
sinon.assert.calledWith(context.setVariable, "transactionId", uuid);
});




This approach verifies that the setVariable method was called with the expected arguments instead of modifying the original functionality.

Hope this helps!




Hi @razvanbulzan - Thanks for the reply.
However i am still getting the same error message any which way i implement it.
By the way i am using this cmd "Mocha test" to run the tests.
Any leads would be useful.
Thanks & Best regards,
Amitt Nerrkar


razvanbulzan
Former Googler

Then maybe you're using 

context.setVariable

Somewhere else in your proxy definition, as a function, 

Most likely

    context.setVariable("traceId", uuid);
    context.setVariable("transactionId", uuid);

 

Try removing them, or adding print statements in order to see if they executed and debug, or replace that for a print and see if it executes, hope this helps!