Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda