Open williscool opened 4 years ago
I don’t have time to review this today; but you might like to look at the tests in Graphile Starter.l, a few of those involve worker methinks.
Since the worker tasks are just functions, do you actually want to test the DB infrastructure around them or do you just want to test the function itself? It seems that we could expose makeJobHelpers
then you could do something like:
it('does things', async (done) => {
const client = /* get client from pool */
const withPgClient = (cb) => cb(client);
const job = mockJob({task_identifier: 'hello', payload: {...payload...}})
const helpers = makeJobHelpers({...options...}, job, {withPgClient});
const spy = jest.spyOn(global.console, 'info');
await hello(job.payload, helpers);
expect(spy.mock.calls[0][4].includes(payloadName)).toBeTruthy();
});
I think it makes sense to include mockJob as part of graphile-worker because the Job
definition may change over time. We can apply defaults to most of the fields, the only required one would be task_identifier
.
What do you think?
yeah all i want to test is that given a certain payload to a task the things I want it to do are done.
I want the db stuff as extracted away as possible.
also I had a lot of flakiness in my tests until I added DROP SCHEMA graphile_worker CASCADE;
to a beforeEach
on all of my tests that use the workers. has been really solid ever since
You could just do delete from graphile_worker.jobs;
I think; it might be more efficient time-wise.
updated to waaaay better way of writting spy tests
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.anything(),
expect.anything(),
expect.stringContaining(payloadName)
);
Just following up on this thread as I want to test one of my workers as well.
Did any tooling end up being exposed to support mocks so that the tests don't require a database connection? (I saw @benjie's thought about exposing makeJobHelpers
but am not sure if that made it into the code base)
(Thank you for the great library!)
makeJobHelpers
is indeed not exposed; I think you would need to mock it anyway.
Was looking through the code for how to test as its not really clear from the readme. Here is something I hacked up.
Would appreciate any feedback on it. Once its decent I would be happy to make a pr. This example is for jest with @babel/preset-env
Thanks for the great lib!