Skip to content

Commit 4a1a092

Browse files
committed
test(ci): fix postgres fail on
1 parent ac2d375 commit 4a1a092

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"nuxt-users": "./dist/cli.mjs"
5454
},
5555
"scripts": {
56-
"prepack": "nuxt-module-build build && node scripts/post-build.js",
56+
"prepack": "nuxt-module-build build --stub && node scripts/post-build.js",
5757
"postinstall": "node -e \"try { require('fs').chmodSync('./dist/cli.mjs', '755') } catch(e) {}\"",
5858
"predev": "node -v | grep -q 'v22' || (echo 'Please use Node.js v22 for development' && exit 1)",
5959
"dev": "yarn dev:prepare && nuxi dev playground",

src/runtime/server/utils/db.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { createDatabase } from 'db0'
2+
import type { Database } from 'db0'
23
import type { ModuleOptions } from 'nuxt-users/utils'
34

5+
const dbCache = new Map<string, Database>()
6+
7+
export const closeAllDbConnections = async () => {
8+
for (const db of dbCache.values()) {
9+
if (db && typeof (db as any).disconnect === 'function') {
10+
await (db as any).disconnect()
11+
}
12+
}
13+
dbCache.clear()
14+
}
15+
416
export const getConnector = async (name: string) => {
517
try {
618
switch (name) {
@@ -17,15 +29,21 @@ export const getConnector = async (name: string) => {
1729
catch (error) {
1830
if (error instanceof Error && error.message.includes('Cannot resolve')) {
1931
throw new Error(`Database connector "${name}" not found. Please install the required peer dependency:\n`
20-
+ '- For sqlite: yarn add better-sqlite3\n'
21-
+ '- For mysql: yarn add mysql2\n'
32+
+ '- For sqlite: yarn add better-sqlite3\n'
33+
+ '- For mysql: yarn add mysql2\n'
2234
+ '- For postgresql: yarn add pg')
2335
}
2436
throw error
2537
}
2638
}
2739

28-
export const useDb = async (options: ModuleOptions) => {
40+
export const useDb = async (options: ModuleOptions): Promise<Database> => {
41+
const cacheKey = JSON.stringify(options.connector)
42+
43+
if (dbCache.has(cacheKey)) {
44+
return dbCache.get(cacheKey)!
45+
}
46+
2947
const connectorName = options.connector!.name
3048
const connector = await getConnector(connectorName)
3149

@@ -36,7 +54,9 @@ export const useDb = async (options: ModuleOptions) => {
3654
}
3755

3856
try {
39-
return createDatabase(connector(connectorOptions))
57+
const db = createDatabase(connector(connectorOptions))
58+
dbCache.set(cacheKey, db)
59+
return db
4060
}
4161
catch (error) {
4262
console.warn(`[Nuxt Users] ⚠️ Failed to connect to ${connectorName} database:`, error instanceof Error ? error.message : 'Unknown error')
@@ -54,4 +74,4 @@ export const checkTableExists = async (options: ModuleOptions, tableName: string
5474
// Table doesn't exist or connection failed
5575
return false
5676
}
57-
}
77+
}

test/test-setup.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ export const cleanupTestSetup = async (dbType: DatabaseType, db: Database, clean
8888
if (db) {
8989
try {
9090
await db.sql`DROP TABLE IF EXISTS {${tableName}}`
91-
// Close the database connection
92-
const dbWithDisconnect = db as { disconnect?: () => Promise<void> }
93-
if (dbWithDisconnect.disconnect) {
94-
await dbWithDisconnect.disconnect()
95-
}
9691
}
9792
catch {
9893
// Ignore errors during cleanup

test/utils.user.test.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,6 @@ describe('User Utilities (src/utils/user.ts)', () => {
5757
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.users)
5858
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.passwordResetTokens)
5959
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.personalAccessTokens)
60-
61-
// Close the database connection
62-
if (db) {
63-
try {
64-
const dbWithDisconnect = db as { disconnect?: () => Promise<void> }
65-
if (dbWithDisconnect.disconnect) {
66-
await dbWithDisconnect.disconnect()
67-
}
68-
}
69-
catch {
70-
// Ignore errors during cleanup
71-
}
72-
}
7360
})
7461

7562
describe('createUser', () => {

test/vitest.setup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { afterEach } from 'vitest'
2+
import { closeAllDbConnections } from '../src/runtime/server/utils/db'
3+
4+
afterEach(async () => {
5+
await closeAllDbConnections()
6+
})

vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { defineConfig } from 'vitest/config'
2+
import { resolve } from 'node:path'
23

34
export default defineConfig({
45
test: {
56
fileParallelism: false, // This will make all test files run sequentially
7+
setupFiles: ['./test/vitest.setup.ts'],
8+
},
9+
resolve: {
10+
alias: {
11+
'nuxt-users/utils': resolve(__dirname, './src/utils'),
12+
},
613
},
714
})

0 commit comments

Comments
 (0)