Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Unit tests failing with context.getvariable not a function

Unit tests failing with context.getvariable not a function 

import expect from 'expect.js';
import sinon from 'sinon';
import rewire from 'rewire'; // Use import instead of require
import { isIPInRange } from '../../test.js'; // ES module import

// Create a mock context
const fakeContext = {
getVariable: sinon.stub(),
setVariable: sinon.stub(),
};

describe('IP Range Checking', function () {

describe('isIPInRange', function () {

it('should mock context.getVariable for test', function () {
// Arrange: Create mocks for context
fakeContext.getVariable
.withArgs('verifyapikey.VAK-VerifyAPIKey.allowlist')
.returns('192.168.1.0/24,10.0.0.0/8');

fakeContext.getVariable
.withArgs('asapp.shared.trueClientIP')
.returns('192.168.1.50');

// Act: Call the function with the mocked context
const found = isIPInRange('192.168.1.50', ['192.168.1.0/24', '10.0.0.0/8']);

// Assert: Check if IP is in range (based on the mock)
expect(found).to.be.true;
});
});
});
0 3 171
3 REPLIES 3


TypeError: context.getVariable is not a function
at api-gateway-proxy-config/src/sharedflows/auth/sharedflowbundle/resources/jsc/allowlist.js:1:27
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
at async exports.loadFilesAsync (api-gateway-proxy-config/src/sharedflows/node_modules/mocha/lib/esm-utils.js:28:20)
at async singleRun (api-gateway-proxy-config/src/sharedflows/node_modules/mocha/lib/cli/run-helpers.js:149:3)
at async exports.runMocha (api-gateway-proxy-config/src/sharedflows/node_modules/mocha/lib/cli/run-helpers.js:186:5)
at async exports.handler (api-gateway-proxy-config/src/sharedflows/node_modules/mocha/lib/cli/run.js:319:5)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------

Hi @nswapna, thank you for sharing the additional details in your follow-up! To further assist the community in diagnosing and resolving the issue you’re encountering, please consider checking out this guidance with tips to ask questions in the Apigee forum.

Sharing well-formulated questions with enough details will help community members offer more effective assistance. We’ll keep an eye on this thread and look forward to seeing further updates or input from the community. 🙂

Hi @nswapna, during today's office hours, @tyayers addressed your question! You can find the recording here, and his answer starts at minute 46:00 🙂 we hope you'll find it helpful!

Interested in joining future office hours? Register here 🔗