Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda