I have a javascript file index.js which will be deployed as a cloudflare worker. This file contains functions, and contains undeclared variables that will be injected to the scope by cloudflare. example of index.js
function addNumbers(request) {
return request.a + request.b + CONSTANT
}
addEventListener('fetch', async event =>
event.respondWith(addNumbers(event.request)),
)
The constant CONSTANT will be injected to the function scope by cloudflare once this script is deployed. I need write unit-test to test the function addNumbers while mocking the constant in the run time, I also can't use any ES6 syntax in index.js, can't use modules or export or anything as cloudflare requires old school javascript.
I tried to use multiple testing tools, but I end up with the problem that the test file can't call the function addNumbers from the script file. I obviously can't use the import statement since I can't export it from the original file, and I can't use require statement since it fails to recognize the constant CONSTANT and the undeclared function addEventListener. I wonder if somehow there is a way to write a unit-test for such a case.
I'm also using wrangler to build my project if that matters, but couldn't find good documentations of how to write unit tests with it.