Create a Post
Local context variable emulation
Hi, I'm trying to setup unit tests for my Stitch functions that I can run locally on my computer and working on an automated build system that will push them to stitch after the tests pass. Obviously context.functions presents a problem when I'm trying to run tests on my local machine, but I have created a work around. I'm emulating the behavior of context by writing my own local version of context that can execute other js files. It also does its own version of context.values to grab configuration data for me. I haven't needed to create, context.user, or context.request yet, but I may need to eventually write it. I pull context into my Stitch function like so and it allows me to run the Stitch function locally: var context = require('../../PlatformFunctions/context'); I'm attaching two screenshots of context.js in action. The screenshots show the mocha test case, the function/values that are being called, and the test case results. I posted the context.js code below. ***Start Code*** //Supports calling up to five arguments function callExternalFunction(importedFunction, args){ let numberOfArgs = args.length; if(numberOfArgs === 1) return importedFunction(args[0]); else if(numberOfArgs ===2) return importedFunction(args[0], args[1]); else if(numberOfArgs === 3) return importedFunction(args[0], args[1],args[2]); else if(numberOfArgs === 4) return importedFunction(args[0], args[1],args[2],args[3]); else if(numberOfArgs === 5) return importedFunction(args[0], args[1],args[2],args[3],args[4]); else return importedFunction(); } let context = { functions : { execute(functionName,...args){ let importedFunction = require(context.currentDirectory + functionName); return callExternalFunction(importedFunction, args); } }, values : { get(valuesName){ return require(context.currentDirectory + valuesName + ".json"); } }, currentDirectory : process.cwd() + "/" }; module.exports = context; ***End Code*** Feature Request: Anyway, the above context.js file has been very useful for me. I would love it if there was something like this created by you guys that emulates the context behavior locally. It could be a js file you give clients that can run on their local node.js for testing. context.js seems to work for my purposes, but it is only my version of how I think context works (I cannot be absolutely sure I emulated it correctly). I would like it if there was some context.js testing file that is blessed by the Mongo team and kept up to date as new features are added.
Load More