test: Mock bull, fix setInterval capturing memory in tests
Towards #3939
This commit is contained in:
43
server/__mocks__/bull.ts
Normal file
43
server/__mocks__/bull.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
export default class Queue {
|
||||
done() {
|
||||
//
|
||||
}
|
||||
|
||||
on() {
|
||||
//
|
||||
}
|
||||
|
||||
count() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
getDelayedCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
add = function (data: any) {
|
||||
const job = this.createJob(data);
|
||||
|
||||
if (!this.handler) {
|
||||
throw Error(
|
||||
"Mocking version requires handler to be set before first add()"
|
||||
);
|
||||
}
|
||||
|
||||
this.handler(job, this.done);
|
||||
};
|
||||
|
||||
process = function (handler: any) {
|
||||
if (this.handler) {
|
||||
throw Error("Cannot define a handler more than once per Queue instance");
|
||||
}
|
||||
|
||||
this.handler = handler;
|
||||
};
|
||||
|
||||
createJob = function (data: any) {
|
||||
return {
|
||||
data: data,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -17,6 +17,8 @@ if (process.env.DATABASE_URL_TEST) {
|
||||
// so that sequelize uses the test config variables
|
||||
require("@server/database/sequelize");
|
||||
|
||||
jest.mock("bull");
|
||||
|
||||
// This is needed for the relative manual mock to be picked up
|
||||
jest.mock("../queues");
|
||||
|
||||
|
||||
@@ -41,9 +41,13 @@ export function createQueue(
|
||||
queue.on("failed", () => {
|
||||
Metrics.increment(`${prefix}.jobs.failed`);
|
||||
});
|
||||
setInterval(async () => {
|
||||
Metrics.gauge(`${prefix}.count`, await queue.count());
|
||||
Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount());
|
||||
}, 5 * 1000);
|
||||
|
||||
if (env.ENVIRONMENT !== "test") {
|
||||
setInterval(async () => {
|
||||
Metrics.gauge(`${prefix}.count`, await queue.count());
|
||||
Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount());
|
||||
}, 5 * 1000);
|
||||
}
|
||||
|
||||
return queue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user