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

158
Visualizações
How to test knexjs in node?

I have a function that store data into database:

async function save(id, fields) {
  ...
  const exist = await knex('table').select('..').where({ id });

  if (exist) {
     await knex('table').update([...fields...]).where({ id });
  } else {
     await knex('table').insert([...fields...]);
  }
  ....
}

How I test this function? I searched in knex docs but got no answer there.

What are the best practices?

Should I connect to "test" database and execute the database? but I think in this case I'll need to update the schema and the data which is kind annoying.

Should I create transaction every time I change the database and undo after the test complete?

Maybe mock knex? but how I know if the type and the query is correct?

Something else?

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