0

I'm experimenting with auto-generating outcomes using @xstate/test and combining it with puppeteer and jest. Using jest's beforeAll and afterAll hooks I'm calling page.setRequestInterception and registering a callback to intercept network requests and mock their response.

XState generates the pathways that I want to test and from each path I can determine whether the test wants to test what should occur when the API fails or succeeds. I'm stuck on how to properly communicate that back to puppeteer and access that information in my request handler.

chautelly
  • 447
  • 3
  • 14
  • I've currently resolved it by just updating a mutable variable declared locally at the top of the file/module but it doesn't exactly feel right. If jest decided not to run the tests in the same order as they've been declared it would break. – chautelly Feb 16 '20 at 18:01

1 Answers1

0

One option I discovered is using query parameters. The Request object that is passed to the event handler has access to the frame that made the request.

In my test:

it(..., async () => {
  await page.goto('http://localhost:3000?failApi=true')
  ..

and in my request handler:

page.on('request', req => {
   // check req.frame.url() here and make choices
...
chautelly
  • 447
  • 3
  • 14