1- import { jest , afterEach , beforeAll } from "@jest/globals " ;
1+ import { NextFunction } from "express " ;
22import { drizzle } from "drizzle-orm/pglite" ;
3- import { pushSchema } from "drizzle-kit/api " ;
3+ import { jest , afterEach , beforeAll , afterAll } from "@jest/globals " ;
44import * as schema from "@repo/database/schema" ;
5- import { db } from "@repo/database" ;
5+ import { db , dbPush , dbReset } from "@repo/database" ;
6+ import redis from "@repo/backend/redis" ;
67
78jest . mock ( "@repo/database" , ( ) => {
89 const originalModule =
@@ -17,27 +18,32 @@ jest.mock("@repo/database", () => {
1718 } ;
1819} ) ;
1920
21+ jest . mock ( "@repo/backend/middlewares/rate-limit" , ( ) => {
22+ const originalModule = jest . requireActual <
23+ typeof import ( "@repo/backend/middlewares/rate-limit" )
24+ > ( "@repo/backend/middlewares/rate-limit" ) ;
25+
26+ const rateLimitHandler = jest
27+ . fn ( )
28+ . mockReturnValue ( ( _ : Request , __ : Response , next : NextFunction ) => {
29+ next ( ) ;
30+ } ) ;
31+
32+ return {
33+ ...originalModule ,
34+ __esModule : true ,
35+ rateLimitHandler,
36+ } ;
37+ } ) ;
38+
2039beforeAll ( async ( ) => {
21- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22- const { apply } = await pushSchema ( schema , db as any ) ;
23- await apply ( ) ;
40+ await dbPush ( db ) ;
2441} ) ;
2542
2643afterEach ( async ( ) => {
27- console . log ( "🗑️ Emptying the entire database" ) ;
28-
29- const tableSchema = db . _ . schema ;
30- if ( ! tableSchema ) throw new Error ( "No table schema found" ) ;
31-
32- const queries = Object . values ( tableSchema ) . map ( ( table ) => {
33- // console.log(`🧨 Preparing delete query for table: ${table.dbName}`);
34- return table . tsName ;
35- } ) ;
36- // console.log(queries);
37-
38- queries . forEach ( async ( query ) => {
39- const schemaToDelete = schema [ query ] ;
40- if ( ! schemaToDelete ) throw new Error ( `No schema found for ${ query } ` ) ;
41- await db . delete ( schemaToDelete ) ;
42- } ) ;
44+ await dbReset ( db ) ;
45+ } ) ;
46+
47+ afterAll ( async ( ) => {
48+ redis . quit ( ) ;
4349} ) ;
0 commit comments