Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support delay modes (ctx.delay) #538

Merged
merged 4 commits into from
Jan 14, 2021
Merged

Support delay modes (ctx.delay) #538

merged 4 commits into from
Jan 14, 2021

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jan 13, 2021

Motivation

Using ctx.delay(Infinity) executes the response timer immediately instead of delaying the response indefinitely, as expected. That's due to how setTimeout (that powers response callback to the worker) operates in JavaScript. These changes aim to add support for infinite response delays and add some safeguards against using large integers as the ctx.delay inpt.

Changes

  • ctx.delay now supports delay modes—enums that abstract specific delay behaviors:
    • real, uses a random realistic server response time.
    • infinite, uses a maximum allowed integer as a setTimeout duration to delay a response indefinitely (useful when emulating pending/loading states).
  • ctx.delay now throws an exception when given Infinity, Number.MAX_VALUE, Number.MAX_SAFE_INTEGER, or any integer value larger than the maximum allowed setTimeout duration.
  • ctx.delay now throws an exception when given an unknown delay mode.

Usage

ctx.delay() // realistic response time
ctx.delay(500) // explicit response time
ctx.delay('real') // the same as `ctx.delay()`
ctx.delay('infinite') // infinite response time

@kettanaito kettanaito marked this pull request as draft January 13, 2021 12:51
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 13, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 45a2e13:

Sandbox Source
MSW React Configuration

timdeschryver
timdeschryver previously approved these changes Jan 13, 2021
@kettanaito
Copy link
Member Author

Failing to test an infinite delay scenario in Puppeteer, because it throws an exception Target is closed when attempting to close a browser with a pending network request.

@kettanaito
Copy link
Member Author

I've added only the "real" integration test, others seem to be too brittle.

@kettanaito kettanaito marked this pull request as ready for review January 14, 2021 09:13
@kettanaito
Copy link
Member Author

Experienced a failing test. Passes locally.

 FAIL  msw-api/setup-server/scenarios/fall-through.node.test.ts (60.105 s)
  ● falls through all relevant handler even if none returns response

    : Timeout - Async callback was not invoked within the 60000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 60000 ms timeout specified by jest.setTimeout.Error:

      41 | })
      42 | 
    > 43 | test('falls through all relevant handler even if none returns response', async () => {
         | ^
      44 |   const res = await fetch('https://test.mswjs.io/blog/article', {
      45 |     method: 'POST',
      46 |   })

      at new Spec (../node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (msw-api/setup-server/scenarios/fall-through.node.test.ts:43:1)

@kettanaito kettanaito merged commit 9b667c7 into master Jan 14, 2021
@kettanaito kettanaito deleted the ctx-delay-enums branch January 14, 2021 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants