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

157
Views
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
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!