How can I properly mock neon with Jest in a Next.js application?
I need to add a unit test for this query:
import { neon } from '@neondatabase/serverless'
const sql = neon(process.env.DATABASE_URL as string)
// Get the total count of posts
export async function getTotalPostCount() {
const data = await sql
SELECT COUNT(*) as count FROM posts
return data[0]?.count ? data[0]?.count - 1 : 0
}
4 Replies
sunny-green•7mo ago
you could branch a database and run that query every time
fair-roseOP•7mo ago
Thanks. So instead of mocking neon I run a query on the branched database from the unit test file?
sunny-green•7mo ago
you could do either. if this is on CI/CD you can rely on branching. local unit tests will want jest mocks. but your mocks will fake returning query results, its an odd thing to test.
IMO
fair-roseOP•7mo ago
Thanks for the clarification, I will skip the mocks and use branching instead