mikecann / mikecann.blog

Mike Cann's Blog
https://mikecann.blog
4 stars 1 forks source link

Tinkering With Convex #153

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Tinkering With Convex - mikecann.co.uk

BattleTabs

https://mikecann.co.uk/posts/tinkering-with-convex

ianmacartney commented 1 year ago

Great post! This got me thinking about how I personally have been testing my Convex apps. I'm curious what you come up with too - probably worth a post on Stack with tips and tricks if you have any already. One thing I'm playing with tonight, for functions where you can avoid DB writes:

  1. Write a test in a internalQuery that calls the function(s) in question, with (read-only) access to the DB, returning the result
  2. Run it with npx convex run --watch testing:testMyFunction
  3. Try out a few different parameters: every time you save the function or parameters, it'll automatically deploy the new code, run it, and show you the new result.
  4. As you debug, any console.log lines will also show up in the convex run results.
  5. Once it's working, add it to a top-level internalAction along with other tests you've written, that you can run from a dedicated CI instance after it does some test data seeding. You can then choose which ones you run in parallel with a simple Promise.all.

Your CI script then just looks like a npx convex run command, which includes the build, bundle, deploy, and test steps in one! It's not perfect, but it's a start. One obvious gap here is having a ready-made database mock to pass into top-level functions to do dependency-injection-style unit testing. If anyone reading this makes one, let me know!

mikecann commented 1 year ago

Interesting idea @ianmacartney thanks for sharing! I wonder if there is a way to then have it automatically cache the test so you no longer have to run it unless that specific function changes 🤔

Ultimately I would love to see a mock DB of some sort so we can just write some fast unit tests then sprinkle a few end-to-end and integration tests in there for good measure.

villelahdenvuo commented 1 year ago

So I made the leap to Postrgres which had the best of both, relational tables AND JSON columns.

I've been saying this ever since I started working on a real work project and never looked back. Now I'm using DynamoDB for the first time.

ianmacartney commented 1 year ago

The fun part of convex queries is the results are trivially cache-able. I could even imagine a meta-query that runs all the query tests, and is constantly subscribed. So long as the subscription is active, it'll re-run on any new code that the queries depend on automatically. It'd require having a long-running CI that's holding the queries open though. And I think mutations are the more interesting things to test generally, but definitely a fun thought experiment