Skip to content

Commit

Permalink
Merge 5752727 into ad1fc3c
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Nov 20, 2019
2 parents ad1fc3c + 5752727 commit f995c21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/classes/job.ts
Expand Up @@ -144,7 +144,12 @@ export class Job {
}
}

toJSON(): JobJson {
toJSON() {
const { queue, ...withoutQueue } = this;
return withoutQueue;
}

asJSON(): JobJson {
return {
id: this.id,
name: this.name,
Expand Down Expand Up @@ -452,7 +457,7 @@ export class Job {
private addJob(client: IORedis.Redis): string {
const queue = this.queue;

const jobData = this.toJSON();
const jobData = this.asJSON();

return Scripts.addJob(client, queue, jobData, this.opts, this.id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/classes/sandbox.ts
Expand Up @@ -6,7 +6,7 @@ const sandbox = (processFile: any, childPool: any) => {

child.send({
cmd: 'start',
job: job.toJSON(),
job: job.asJSON(),
});

const done = new Promise((resolve, reject) => {
Expand Down
23 changes: 23 additions & 0 deletions src/test/test_job.ts
Expand Up @@ -67,6 +67,29 @@ describe('Job', function() {
});
});

describe('JSON.stringify', () => {
it('retains property types', async () => {
const data = { foo: 'bar' };
const job = await Job.create(queue, 'test', data);
job.returnvalue = 1;
job.progress = 20;
const json = JSON.stringify(job);
const parsed = JSON.parse(json);
expect(parsed).to.have.deep.property('data', data);
expect(parsed).to.have.property('name', 'test');
expect(parsed).to.have.property('returnvalue', 1);
expect(parsed).to.have.property('progress', 20);
});

it('omits the queue property to avoid a circular json error on node 8', async () => {
const data = { foo: 'bar' };
const job = await Job.create(queue, 'test', data);
const json = JSON.stringify(job);
const parsed = JSON.parse(json);
expect(parsed).not.to.have.property('queue');
});
});

describe('.update', function() {
it('should allow updating job data', async function() {
const job = await Job.create(queue, 'test', { foo: 'bar' });
Expand Down

0 comments on commit f995c21

Please sign in to comment.