jawj / zapatos

Zero-abstraction Postgres for TypeScript: a non-ORM database library
https://jawj.github.io/zapatos/
Other
1.3k stars 46 forks source link

mocking zapatos in unit tests #176

Open leoschweizer opened 5 months ago

leoschweizer commented 5 months ago

This is basically an open question to all zapatos users: how do you mock zapatos in unit tests? I'm currently trying to find a setup for esm and the native node test runner. The obvious approach of

import { mock } from 'node:test'
import * as db from 'zapatos/db'

mock.method(db, 'select')

does not work due to:

  TypeError [Error]: Cannot redefine property: select
      at defineProperty (<anonymous>)
      at MockTracker.method (node:internal/test_runner/mock/mock:404:5)

Previously I successfully used jest, but want to get rid of it:

jest.mock('zapatos/db', () => {
    const original = jest.requireActual('zapatos/db')
    return {
        ...original,
        select: jest.fn(),
    }
})

The native module mocking is still experimental in node and I did not have any success with that either.

So, how do you do it?