Skip to content

Commit

Permalink
fix: correctly handle "falsy" data values fixes #264
Browse files Browse the repository at this point in the history
  • Loading branch information
manast authored Dec 21, 2020
1 parent ff318de commit cf1dbaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class Job<T = any, R = any, N extends string = string> {
return {
id: this.id,
name: this.name,
data: JSON.stringify(this.data || {}),
data: JSON.stringify(typeof this.data === 'undefined' ? {} : this.data),
opts: JSON.stringify(this.opts),
progress: this.progress,
attemptsMade: this.attemptsMade,
Expand Down
31 changes: 30 additions & 1 deletion src/test/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ describe('Job', function() {
const parsed = JSON.parse(json);
expect(parsed).not.to.have.property('queue');
});

it('should correctly handle zero passed as data', async () => {
const data = 0;
const job = await Job.create(queue, 'test', data);
const json = JSON.stringify(job);
const parsed = JSON.parse(json);
expect(parsed).to.have.deep.property('data', data);

const newQueue = new Queue(queueName);
let worker: Worker;
const promise = new Promise<void>(async (resolve, reject) => {
worker = new Worker(queueName, async job => {
try {
expect(job.data).to.be.equal(0);
} catch (err) {
reject(err);
}
resolve();
});
const testJob = await newQueue.add('test', 0);
});

try {
await promise;
} finally {
await newQueue.close();
worker && (await worker.close());
}
});
});

describe('.update', function() {
Expand Down Expand Up @@ -298,7 +327,7 @@ describe('Job', function() {

const completed: string[] = [];

const done = new Promise(resolve => {
const done = new Promise<void>(resolve => {
worker.on('completed', job => {
completed.push(job.id);
if (completed.length > 3) {
Expand Down

0 comments on commit cf1dbaf

Please sign in to comment.