Repository: taskforcesh/bullmq
Pull request #133 (2020-02-10 18:31:34)

fix: modified QueueGetters.getJob and Job.fromId to also return null
Fixes: https://github.com/taskforcesh/bullmq/issues/130


Patch:
diff --git a/src/classes/job.ts b/src/classes/job.ts
index b8170dbacb..3ea4450a63 100644
--- a/src/classes/job.ts
+++ b/src/classes/job.ts
@@ -135,12 +135,15 @@ export class Job<T = any, R = any> {
     return job;
   }
 
-  static async fromId(queue: QueueBase, jobId: string) {
+  static async fromId(
+    queue: QueueBase,
+    jobId: string,
+  ): Promise<Job | undefined> {
     // jobId can be undefined if moveJob returns undefined
     if (jobId) {
       const client = await queue.client;
       const jobData = await client.hgetall(queue.toKey(jobId));
-      return isEmpty(jobData) ? null : Job.fromJSON(queue, jobData, jobId);
+      return isEmpty(jobData) ? undefined : Job.fromJSON(queue, jobData, jobId);
     }
   }
 
diff --git a/src/classes/queue-getters.ts b/src/classes/queue-getters.ts
index a31c4ab746..b6c3833d9c 100644
--- a/src/classes/queue-getters.ts
+++ b/src/classes/queue-getters.ts
@@ -6,7 +6,7 @@ import { Job } from './job';
 import { clientCommandMessageReg } from './worker';
 
 export class QueueGetters extends QueueBase {
-  getJob(jobId: string) {
+  getJob(jobId: string): Promise<Job | undefined> {
     return Job.fromId(this, jobId);
   }
 


Test patch:
diff --git a/src/test/test_getters.ts b/src/test/test_getters.ts
index 5983acbad6..82703ace26 100644
--- a/src/test/test_getters.ts
+++ b/src/test/test_getters.ts
@@ -69,18 +69,18 @@ describe('Jobs getters', function() {
     await worker.close();
   });
 
-  /*
-  it('should get a specific job', function(done) {
-    var data = { foo: 'sup!' };
-    queue.add(data).then(function(job) {
-      queue.getJob(job.id).then(function(returnedJob) {
-        expect(returnedJob.data).to.eql(data);
-        expect(returnedJob.id).to.be.eql(job.id);
-        done();
-      });
-    });
+  it('should get a specific job', async () => {
+    const data = { foo: 'sup!' };
+    const job = await queue.add('test', data);
+    const returnedJob = await queue.getJob(job.id);
+    expect(returnedJob.data).to.eql(data);
+    expect(returnedJob.id).to.be.eql(job.id);
+  });
+
+  it('should get undefined for nonexistent specific job', async () => {
+    const returnedJob = await queue.getJob('test');
+    expect(returnedJob).to.be.equal(undefined);
   });
-  */
 
   it('should get completed jobs', async () => {
     const worker = new Worker(queueName, async job => {});
diff --git a/src/test/test_job.ts b/src/test/test_job.ts
index 4a6d2928a4..38b9f5971d 100644
--- a/src/test/test_job.ts
+++ b/src/test/test_job.ts
@@ -101,7 +101,7 @@ describe('Job', function() {
       const job = await Job.create(queue, 'test', { foo: 'bar' });
       await job.remove();
       const storedJob = await Job.fromId(queue, job.id);
-      expect(storedJob).to.be.equal(null);
+      expect(storedJob).to.be.equal(undefined);
     });
   });
 
diff --git a/src/test/test_worker.ts b/src/test/test_worker.ts
index 11fdae5886..942e9c633d 100644
--- a/src/test/test_worker.ts
+++ b/src/test/test_worker.ts
@@ -57,7 +57,7 @@ describe('workers', function() {
         worker.on('completed', async (job: Job) => {
           try {
             const gotJob = await queue.getJob(job.id);
-            expect(gotJob).to.be.equal(null);
+            expect(gotJob).to.be.equal(undefined);
             const counts = await queue.getJobCounts('completed');
             expect(counts.completed).to.be.equal(0);
             await worker.close();
@@ -89,7 +89,7 @@ describe('workers', function() {
         worker.on('completed', async job => {
           try {
             const gotJob = await newQueue.getJob(job.id);
-            expect(gotJob).to.be.equal(null);
+            expect(gotJob).to.be.equal(undefined);
             const counts = await newQueue.getJobCounts('completed');
             expect(counts.completed).to.be.equal(0);
             await worker.close();
@@ -130,10 +130,10 @@ describe('workers', function() {
                 const job = await queue.getJob(jobId);
                 const logs = await queue.getJobLogs(jobId);
                 if (index >= datas.length - keepJobs) {
-                  expect(job).to.not.be.equal(null);
+                  expect(job).to.not.be.equal(undefined);
                   expect(logs.logs).to.not.be.empty;
                 } else {
-                  expect(job).to.be.equal(null);
+                  expect(job).to.be.equal(undefined);
                   expect(logs.logs).to.be.empty;
                 }
               }),
@@ -176,9 +176,9 @@ describe('workers', function() {
                 jobIds.map(async (jobId, index) => {
                   const job = await newQueue.getJob(jobId);
                   if (index >= datas.length - keepJobs) {
-                    expect(job).to.not.be.equal(null);
+                    expect(job).to.not.be.equal(undefined);
                   } else {
-                    expect(job).to.be.equal(null);
+                    expect(job).to.be.equal(undefined);
                   }
                 }),
               );
@@ -215,7 +215,7 @@ describe('workers', function() {
           await queue
             .getJob(jobId)
             .then(job => {
-              expect(job).to.be.equal(null);
+              expect(job).to.be.equal(undefined);
               return null;
             })
             .then(() => {
@@ -248,7 +248,7 @@ describe('workers', function() {
       return new Promise((resolve, reject) => {
         worker.on('failed', async jobId => {
           const job = await newQueue.getJob(jobId);
-          expect(job).to.be.equal(null);
+          expect(job).to.be.equal(undefined);
           const counts = await newQueue.getJobCounts('completed');
           expect(counts.completed).to.be.equal(0);
           await worker.close();
@@ -285,9 +285,9 @@ describe('workers', function() {
               jobIds.map(async (jobId, index) => {
                 const job = await queue.getJob(jobId);
                 if (index >= datas.length - keepJobs) {
-                  expect(job).to.not.be.equal(null);
+                  expect(job).to.not.be.equal(undefined);
                 } else {
-                  expect(job).to.be.equal(null);
+                  expect(job).to.be.equal(undefined);
                 }
               }),
             );
@@ -330,9 +330,9 @@ describe('workers', function() {
                 jobIds.map(async (jobId, index) => {
                   const job = await newQueue.getJob(jobId);
                   if (index >= datas.length - keepJobs) {
-                    expect(job).to.not.be.equal(null);
+                    expect(job).to.not.be.equal(undefined);
                   } else {
-                    expect(job).to.be.equal(null);
+                    expect(job).to.be.equal(undefined);
                   }
                 }),
               );