Closed odigity closed 7 years ago
Hey @odigity, that looks quite nice! How would you run an assertion though if the transaction is still pending?
knex.transaction()
look like it might return a promise (given the .catch()
). Perhaps you don't need to return a separate promise, though if you do perhaps do return new Promise
and wire up the resolve
within the Promise
executor method.
Would you be interested in turning this into a recipe?
Either that, or package it on NPM. Too busy to factor it out right now, but it's on my todo list. That would at least provide a central location for discussion on unit-testing with Ava + Knex.
Closing due to inactivity.
@odigity I recently completed a contract where I used Knex, but I never ended up writing tests for the application code. Still, if you have the time would be great to see a recipe on how to approach this.
I'm also interested in unit testing knex + ava, will let you guys know if/when I put something together and link it here
I'm starting a new project for automating the seeding on multiples db with javascript and knex.js, please have a look at this and let me hear your recommends.
here is a code example:
//creating and seeding process
ks.createAndSeed(userTableModel, 10).then(() => {
//creating the table automatically & seeding ...
ks.createAndSeed_close(roleTableModel, 10, (table) => { //closes the connection after process
//creating the table with knex.js fn & and seeding ...
table.increments(),
table.string('name'),
table.string('category'),
table.timestamps(true, true)
}).then(()) =>{
//do something here...
}
})
I recently started my first serious JavaScript project. After poking around, I picked Knex.js to talk to MySQL, and it's been pretty nice. Last week I decided it was time to add a unit testing library, and happened to come across a blog post from someone switching from Sinon.JS to AVA, so I decided to give AVA a try, and it's been pretty nice.
However, I could't find much information around techniques and best practices for writing unit tests for DB code on the AVA site, nor for writing unit tests around Knex.js code on the Knex site - nor did googling turn up anything reasonable. (Also, the coverage of transactions on Knex is extremely minimal and ambiguous.)
So, despite being new to Knex, Ava, Promises, and async, I took a stab at implementing the strategy used by most Ruby frameworks, which is wrapping each test in a transaction and rolling back at the end.
I opened an issue on the Knex repo to solicit feedback:
https://github.com/tgriesser/knex/issues/2076
And here's a direct link to the code - which is written in a test-framework-agnostic way (because I was posting it to a non-AVA forum), but you can see it basically mimics an AVA test file:
https://gist.github.com/odigity/7f37077de74964051d45c4ca80ec3250
I'm interested in feedback (did I do anything stupid?), and am happy to contribute this example for docs purposes if you think it will be useful to others.