Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How to write unit-tests for a javascript file written to be deployed as a cloudflare worker

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!