Created
August 26, 2021 15:27
-
-
Save robertherber/465ea9ba69cd8d615f0ee4d7e512bbb4 to your computer and use it in GitHub Desktop.
Bull Mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ObjectId } from 'mongodb'; | |
import type Bull from 'bull'; | |
const fakeJob = (data, opts): Bull.Job => { | |
const dataSerialized = JSON.parse(JSON.stringify(data)); | |
const job = { | |
data: dataSerialized, | |
opts, | |
id: opts?.jobId || new ObjectId().toHexString(), | |
progress: jest.fn(), | |
attemptsMade: 0, | |
} as unknown as Bull.Job; | |
return job; | |
}; | |
export default class CustomQueue { | |
name: string | |
constructor(name) { | |
this.name = name; | |
} | |
processFn: Bull.Queue['process'] | |
process = (fn) => { | |
console.log(`Registered function ${this.name}`); | |
this.processFn = fn; | |
} | |
add: Bull.Queue['add'] = async (data, opts) => { | |
const job = fakeJob(data, opts); | |
console.log(`Running ${this.name}`, data); | |
// eslint-disable-next-line | |
// @ts-ignore | |
const results = await this.processFn(job); | |
console.log(`Done running ${this.name}`, results); | |
return job; | |
} | |
removeJobs = jest.fn(); | |
// eslint-disable-next-line | |
// @ts-ignore | |
addBulk: Bull.Queue['addBulk'] = async (array) => { | |
console.log(`AddBulk ${this.name}`); | |
// eslint-disable-next-line | |
// @ts-ignore | |
const result = await Promise.all(array.map(async ({ data, opts }) => { | |
const job = fakeJob(data, opts); | |
// eslint-disable-next-line | |
// @ts-ignore | |
await this.processFn(job); | |
return job; | |
})); | |
console.log(`addBulk Done: ${this.name}`); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment