timgit / pg-boss

Queueing jobs in Postgres from Node.js like a boss
MIT License
2.04k stars 157 forks source link

Advice on how to write integration tests with jes #183

Closed cpgo closed 4 years ago

cpgo commented 4 years ago

Hello, im trying to write a test for my job handler. Idealy I tried to simply instantiate my handler and pass a payload to it but that didnt work because the parameter that pg-boss passes to it (This is what I ended up doing, see my other comment)

async run(job: JobWithDoneCallback<unknown, unknown>): Promise<JobWithDoneCallback<unknown, string>> {

Since I cannot instantiate a JobWithDoneCallback I am trying to simply call pg-boss on my tests.

    await worker.pgBoss.publish(queue, firstDeployment)

    await worker.pgBoss.subscribe(queue, async(job) => {
      await deploymentHandler.run(job)
      await worker.pgBoss.onComplete(job.id, async() => {
        const jobDeployment = job.data as DeploymentEntityV2
        const handledDeployment = await manager.findDeployment(jobDeployment.id)
        expect(handledDeployment.components[0].running).toEqual(true)
      })
    })

This is what I tried so far, but my expects are not running (or are only in the context of pg-boss?)

cpgo commented 4 years ago

Well... I was overthinking this.

I could simply do this

    const jobWithDoneCallback : JobWithDoneCallback<unknown, unknown>= {
      data: firstDeployment,
      done: () => ({}),
      id: 'job-id',
      name: 'job-name'
    }

    const jobResult = await deploymentHandler.run(jobWithDoneCallback)