jeudi 4 janvier 2018

NodeJS test continuing async flow with Jasmine 2.4

My route calls a function startJob() that immediately returns a response "Job started." It also calls a function runJob(), which continues with the job flow (async).

Obviously, I can't just do const jobResults = await startJob(), since that function just returns "Job started." The actual results of the job are not available until all the subsequent functions complete.

So I need a way to check the results of the test after all the functions complete, meaning I need to know when all the functions complete.

The last function in the flow logResults() is not going to work to spy on, because I need it to call through (including an async call). Jasmine doesn't allow me to call it through and then call another function that checks the results.

My idea was to add a function at the end of logResults() called finishJob and spy on that. finishJob() wouldn't do anything, so I wouldn't need to call it through. Pseudo-code: spyOn(finishJob).and.callFake( [function that checks the results] )

However, it seems like bad practice to add a function to the code purely for testing reasons.

Other possible solutions would be using some kind of node watchers, a state change machine, or just doing a timeout (also bad practice? but quick implementation...)

Aucun commentaire:

Enregistrer un commentaire