Skip to content

Commit

Permalink
fix(msw): make delay optional (#1360)
Browse files Browse the repository at this point in the history
Co-authored-by: Alfred Jonsson <[email protected]>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored May 9, 2024
1 parent 4536f34 commit 393a6d8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
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
? 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});`
}
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
Expand Down

0 comments on commit 393a6d8

Please sign in to comment.