Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Feb 18, 2024
1 parent 52051aa commit d11392e
Show file tree
Hide file tree
Showing 4 changed files with 404 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/node": "^18.18.3",
"ajv-cli": "^5.0.0",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.5",
"json-schema-to-typescript": "^13.1.2",
"prettier": "^2.8.8",
"ts-jest": "^29.1.2",
Expand Down
51 changes: 51 additions & 0 deletions src/__mocks__/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { mock } from 'jest-mock-extended'
import { IpcWrapper } from '../host-api/ipc-wrapper.js'

const orgSetTimeout = setTimeout
export async function runAllTimers(): Promise<void> {
// Run all timers, and wait, multiple times.
Expand All @@ -16,3 +19,51 @@ export async function runTimersUntilNow(): Promise<void> {
await new Promise((resolve) => orgSetTimeout(resolve, 0))
}
}

const mockOptions = {
fallbackMockImplementation: () => {
throw new Error('not mocked')
},
}

export function createIpcWrapperMock<TOutbound extends { [key: string]: any }, TInbound extends { [key: string]: any }>(
sendWithCb?: jest.Mock<
ReturnType<IpcWrapper<TOutbound, TInbound>['sendWithCb']>,
Parameters<IpcWrapper<TOutbound, TInbound>['sendWithCb']>
>
): IpcWrapper<TOutbound, TInbound> {
return mock<IpcWrapper<TOutbound, TInbound>>(
{
sendWithCb,
},
mockOptions
)
}

export interface ManualPromise<T> extends Promise<T> {
isResolved: boolean
manualResolve(res: T): void
manualReject(e: Error): void
}
// eslint-disable-next-line @typescript-eslint/promise-function-async
export function createManualPromise<T>(): ManualPromise<T> {
let resolve: (val: T) => void = () => null
let reject: (err: Error) => void = () => null
const promise = new Promise<T>((resolve0, reject0) => {
resolve = resolve0
reject = reject0
})

const manualPromise: ManualPromise<T> = promise as any
manualPromise.isResolved = false
manualPromise.manualReject = (err) => {
manualPromise.isResolved = true
return reject(err)
}
manualPromise.manualResolve = (val) => {
manualPromise.isResolved = true
return resolve(val)
}

return manualPromise
}
Loading

0 comments on commit d11392e

Please sign in to comment.