From 42a7e89b06ddbcaaf00d238128946503efeb5608 Mon Sep 17 00:00:00 2001 From: Kiss Miklos Date: Mon, 26 Sep 2022 11:40:18 +0200 Subject: [PATCH] add test for global addition Signed-off-by: Kiss Miklos --- .../lib/templating/SecureTemplater.test.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/plugins/scaffolder-backend/src/lib/templating/SecureTemplater.test.ts b/plugins/scaffolder-backend/src/lib/templating/SecureTemplater.test.ts index 8b8d7847ac..76643131f5 100644 --- a/plugins/scaffolder-backend/src/lib/templating/SecureTemplater.test.ts +++ b/plugins/scaffolder-backend/src/lib/templating/SecureTemplater.test.ts @@ -144,6 +144,27 @@ describe('SecureTemplater', () => { ['the input value', 'another extra arg'], ]); }); + it('should make additional globals available when requested', async () => { + const mockGlobal1 = jest.fn(() => 'awesome global function'); + const mockGlobal2 = 'foo'; + const mockGlobal3 = 123456; + const renderWith = await SecureTemplater.loadRenderer({ + additionalTemplateGlobals: { mockGlobal1, mockGlobal2, mockGlobal3 }, + }); + const renderWithout = await SecureTemplater.loadRenderer(); + + const ctx = {}; + + expect(renderWith('${{ mockGlobal1() }}', ctx)).toBe( + 'awesome global function', + ); + expect(renderWith('${{ mockGlobal2 }}', ctx)).toBe('foo'); + expect(renderWith('${{ mockGlobal3 }}', ctx)).toBe('123456'); + + expect(() => renderWithout('${{ mockGlobal1() }}', ctx)).toThrow( + /Error: Unable to call `mockGlobal1`/, + ); + }); it('should not allow helpers to be rewritten', async () => { const render = await SecureTemplater.loadRenderer({