We export a "global" database object called db from a file called db.js. This file contains all the knex connection info. db is used across our system in basically every database interaction.
When adding Jest for testing, I realized that I needed to recreate a knex object in each test script and not use the global db object. However, the functions I want to test that are part of our system still use db. So, I get connection errors because it's not using the active connection which is established in the test script.
Is there a best practice or a straightforward way to test my system functions? Do I even need to recreate the connection in each script (I get connection errors if I don't)? Or do I need to add an argument to all the functions for the db object so that I can use my new connection?
Using Node, Jest, and Postgres. The "connection errors" I get are "role 'dan' does not exist", where 'dan' is my operating system login name.