Skip to content

Commit

Permalink
test(passthrough): warn developer in debug mode that tests will fail
Browse files Browse the repository at this point in the history
It's useful to run tests in debug mode because it opens a live ("headful")
Playwright browser, e.g. `DEBUG=1 yarn test:integration passthrough` will
run the `passthrough.test.ts` tests in a headful Chromium instance

However, the live debug browser behaves slightly differently from
the headless browser, because there is a navigation request to the
page where the service worker has already been loaded, which causes
the service worker to intercept the initial navigation request.

This is a dangerous trap if you're trying to fix a test by running
it in debug mode. If this is detected, print a friendly warning to
the developer to hopefully save them some time.
  • Loading branch information
milesrichardson committed Sep 1, 2022
1 parent a59686d commit 8076ddf
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/msw-api/req/passthrough.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ResponseBody {
}

function prepareRuntime() {
warnDeveloperInDebugModeThatTestsAreExpectedToFail()
return pageWith({
example: path.resolve(__dirname, 'passthrough.mocks.ts'),
})
Expand Down Expand Up @@ -119,3 +120,23 @@ it('prints a warning and performs a request as-is if nothing was returned from t
]),
)
})

/*
* If tests are running with DEBUG=1 (with a live/"headful" Playwright browser),
* print one friendly warning to avoid frustration of apparently unfixable tests
*/
let warned: string | undefined
const warnDeveloperInDebugModeThatTestsAreExpectedToFail = () => {
if (!process.env.DEBUG || warned) {
return
}

warned = [
'WARNING [passthrough.test.ts]: these tests will FAIL with DEBUG enabled!',
'\tOpening the Playwright debug browser interferes with the test, because',
'\tit triggers a navigation request that the service worker intercepts,',
'\tproducing an unexpected and unhandled request, but only in debug mode.',
].join('\n')

console.warn(warned)
}

0 comments on commit 8076ddf

Please sign in to comment.