From 03c4540f36ddc0c5666d2603f58d71aca841ef5b Mon Sep 17 00:00:00 2001 From: Samuel Male Date: Tue, 8 Oct 2024 15:34:20 +0300 Subject: [PATCH] (fix) Expression evaluator should use typeof not instanceof to detect functions (#1174) --- .../esm-expression-evaluator/src/evaluator.test.ts | 6 ++++++ .../framework/esm-expression-evaluator/src/evaluator.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/framework/esm-expression-evaluator/src/evaluator.test.ts b/packages/framework/esm-expression-evaluator/src/evaluator.test.ts index 0435a2760..75c45653a 100644 --- a/packages/framework/esm-expression-evaluator/src/evaluator.test.ts +++ b/packages/framework/esm-expression-evaluator/src/evaluator.test.ts @@ -172,6 +172,12 @@ describe('OpenMRS Expression Evaluator', () => { ).resolves.toBe(2); }); + it('Should support mock functions', () => { + expect(evaluate('api.getValue()', { api: { getValue: jest.fn().mockImplementation(() => 'value') } })).toBe( + 'value', + ); + }); + it('Should support real-world use-cases', () => { expect( evaluate('!isEmpty(array)', { diff --git a/packages/framework/esm-expression-evaluator/src/evaluator.ts b/packages/framework/esm-expression-evaluator/src/evaluator.ts index 8d716a073..3a00499cd 100644 --- a/packages/framework/esm-expression-evaluator/src/evaluator.ts +++ b/packages/framework/esm-expression-evaluator/src/evaluator.ts @@ -468,7 +468,7 @@ function visitCallExpression(expression: jsep.CallExpression, context: Evaluatio if (!callee) { throw `No function named ${expression.callee} is defined in this context`; - } else if (!(callee instanceof Function)) { + } else if (!(typeof callee === 'function')) { throw `${expression.callee} is not a function`; }