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

feat(msw): make delay optional #1360

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,12 @@ Use to specify the mock type you want to generate.

#### delay

Type: `Number | Function`.
Type: `Number | Function | false`.

Default Value: `1000`.

Use to specify the delay time for the mock. It can either be a fixed number or a function that returns a number.
Use to specify the delay time for the mock. It can either be a fixed number, false or a function that returns a number.
Setting delay to false removes the delay call completely.

#### delayFunctionLazyExecute

Expand Down Expand Up @@ -1125,9 +1126,10 @@ module.exports = {

##### delay

Type: `number` or `Function`.
Type: `number`, `Function` or `false`.

Give you the possibility to set delay time for mock. It can either be a fixed number or a function that returns a number.
Give you the possibility to set delay time for mock. It can either be a fixed number, false or a function that returns a number.
Setting delay to false removes the delay call completely.

Default Value: `1000`

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ export type GlobalMockOptions = {
useExamples?: boolean;
// This is used to generate mocks for all http responses defined in the OpenAPI specification
generateEachHttpStatus?: boolean;
// This is used to set the delay to your own custom value
delay?: number | (() => number);
// This is used to set the delay to your own custom value, or pass false to disable delay
delay?: false | number | (() => number);
// This is used to execute functions that are passed to the 'delay' argument
// at runtime rather than build time.
delayFunctionLazyExecute?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/mock/src/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getDelay = (
options?: GlobalMockOptions,
): GlobalMockOptions['delay'] => {
const overrideDelay =
typeof override?.mock?.delay === 'number'
override?.mock?.delay !== undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unsure about this, but it feels like override always should override when present, and not only when it's overridden by a number?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Not sure why this decision was made?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it was missed when support for functions was added?

? override?.mock?.delay
: options?.delay;
const delayFunctionLazyExecute =
Expand All @@ -15,6 +15,7 @@ export const getDelay = (
case 'function':
return delayFunctionLazyExecute ? overrideDelay : overrideDelay();
case 'number':
case 'boolean':
return overrideDelay;
default:
return 1000;
Expand Down
10 changes: 7 additions & 3 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ const generateDefinition = (
? `export const ${getResponseMockFunctionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''})${mockData ? '' : `: ${returnType}`} => (${value})\n\n`
: '';

const delayTime = getDelay(override, !isFunction(mock) ? mock : undefined);
const delay = getDelay(override, !isFunction(mock) ? mock : undefined);
const handlerImplementation = `
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
return http.${verb}('${route}', async () => {
await delay(${isFunction(delayTime) ? `(${delayTime})()` : delayTime});
return http.${verb}('${route}', ${
delay === false
? '() => {'
: `async () => {
await delay(${isFunction(delay) ? `(${delay})()` : delay});`
}
Comment on lines +100 to +105
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that the definition itself is unnecessary if delay is false, but what do you think?

Suggested change
return http.${verb}('${route}', ${
delay === false
? '() => {'
: `async () => {
await delay(${isFunction(delay) ? `(${delay})()` : delay});`
}
return http.${verb}('${route}', ${
delay === isFunction(delay) || isNumber(delay)
? `async () => {
await delay(${isFunction(delay) ? `(${delay})()` : delay});`
: ''
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the second argument to http.get must be a function, so we cannot remove the () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the mock handler looks with delay: false
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's nice. That's what I wanted.

return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
Expand Down