We seem to be hitting a repeating understanding issue here. The left-hand argument of a context set function is a JavaScript identifier, not JSON. The data on the right-hand side is stored in memory as a JavaScript variable. But, on writing to file, it is saved as a JSON string.
For this reason, if you are using an in-memory context var, you can put functions in it. But if you write it to a file-based store and restart Node-RED, the function will have gone.
[{"id":"0fcbe41248c8a4b1","type":"inject","z":"b2f18a716bd20f99","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"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","payloadType":"str","x":290,"y":1720,"wires":[["6b51232d72a6fbc7"]]},{"id":"6b51232d72a6fbc7","type":"function","z":"b2f18a716bd20f99","name":"function 27","func":"function fnTest() {\n return 42\n}\nglobal.set('TEST', {\n ans: 42,\n fnTest: fnTest,\n})\n\nglobal.set('TEST2', {\n ans: 42,\n fnTest: fnTest,\n}, 'file')","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":1720,"wires":[[]]},{"id":"9132acdb9e6301f4","type":"inject","z":"b2f18a716bd20f99","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"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","payloadType":"str","x":290,"y":1780,"wires":[["8fbaca02b552f8bc"]]},{"id":"8fbaca02b552f8bc","type":"function","z":"b2f18a716bd20f99","name":"function 28","func":"const TEST = global.get('TEST')\nconst TEST2 = global.get('TEST2', 'file')\n\nreturn {\n test1: {\n fnRes: TEST.fnTest(),\n payload: TEST,\n },\n test2: {\n fnRes: TEST2.fnTest(),\n payload: TEST2,\n },\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":1780,"wires":[["a87ddcc2d1c78d1c"]]},{"id":"a87ddcc2d1c78d1c","type":"debug","z":"b2f18a716bd20f99","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":1780,"wires":[]}]
Similarly if you use the REDIS-backed store.
That's because you cannot write a function to JSON. If you check the global.json file, you can confirm this.
So to repeat, the 1st argument to a context set is a JavaScript identifier, it should be a valid JS variable name. However, there is some extra processing that lets you read from or write to a deep property. This is not standard javascript or json. It is a "fix" that is node-red specific.
If you want to use context for storing an Object, pick a valid name and keep everything in the object itself.
For simplicity I strongly recommend only using simple variable names for context set/get - always get/set the whole variable - you loose nothing and gain nothing by trying to use the "clever" deep references in the name. Do that in standard JavaScript - if you are using a function node. In any case, always use JavaScript/JSON valid variable and property names to save yourself a lot of head scratching.