Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"cache": true
},
"test": {
"dependsOn": [
"^build"
],
"inputs": [
"default",
"^prod"
Expand All @@ -31,9 +34,12 @@
]
},
"test:coverage": {
"dependsOn": [
"^build"
],
"outputs": [
"{projectRoot}/coverage"
]
}
}
}
}
38 changes: 37 additions & 1 deletion packages/provider-venom/__tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, jest, test } from '@jest/globals'
import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals'
import { writeFile } from 'fs/promises'
import { utils } from '@builderbot/bot'
import path from 'path'
Expand Down Expand Up @@ -30,7 +30,26 @@ describe('#VenomProvider', () => {
let mockNext: any
let mockRes: any
let mockReq: any
const activeTimers = new Set<any>()
let originalSetTimeout: typeof setTimeout
let originalClearTimeout: typeof clearTimeout

beforeEach(() => {
// Intercept setTimeout to track all timers
originalSetTimeout = global.setTimeout
originalClearTimeout = global.clearTimeout

global.setTimeout = ((fn: Function, delay?: number, ...args: any[]) => {
const timer = originalSetTimeout(fn, delay, ...args)
activeTimers.add(timer)
return timer
}) as typeof setTimeout

global.clearTimeout = ((timer: any) => {
activeTimers.delete(timer)
return originalClearTimeout(timer)
}) as typeof clearTimeout

venomProvider = new VenomProvider({ name: 'test', gifPlayback: false })
mockReq = {}
mockRes = {
Expand All @@ -42,6 +61,18 @@ describe('#VenomProvider', () => {
mockNext = jest.fn()
})

afterEach(() => {
jest.clearAllMocks()
// Clear all tracked timers
activeTimers.forEach((timer) => {
originalClearTimeout(timer)
})
activeTimers.clear()
// Restore original functions
global.setTimeout = originalSetTimeout
global.clearTimeout = originalClearTimeout
})

describe('VenomProvider Constructor', () => {
test('Initialization with default arguments', () => {
expect(venomProvider.globalVendorArgs).toEqual({
Expand Down Expand Up @@ -591,12 +622,17 @@ describe('#VenomProvider', () => {
emit: mockEmit,
}
venomProvider.emit = (mockEventEmitter as any).emit.bind(mockEventEmitter)
;(venom.create as jest.Mock).mockImplementationOnce(() => {
return Promise.reject(new Error('Initialization failed'))
})

// Act
await venomProvider['initVendor']()

// Assert
expect(venom.create).toHaveBeenCalled()
expect(mockEmit).toHaveBeenCalled()
// The setTimeout created in the error handler will be automatically cleaned up in afterEach
})
})
})
Loading