Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: apply response delay conditionally (#1300)
* fix(handleRequest): apply response delay conditionally * test: add integration test for delay in node
- Loading branch information
1 parent
e05157c
commit 496154d
Showing
3 changed files
with
108 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
import fetch from 'node-fetch' | ||
import { rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import { performance } from 'perf_hooks' | ||
|
||
const server = setupServer() | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
async function makeRequest(url: string) { | ||
const requestStart = performance.now() | ||
const res = await fetch(url) | ||
const requestEnd = performance.now() | ||
const responseTime = requestEnd - requestStart | ||
|
||
return { res, responseTime } | ||
} | ||
|
||
test('uses explicit server response time', async () => { | ||
server.use( | ||
rest.get('http://localhost/user', (req, res, ctx) => { | ||
return res(ctx.delay(500), ctx.text('john')) | ||
}), | ||
) | ||
|
||
const { res, responseTime } = await makeRequest('http://localhost/user') | ||
|
||
expect(responseTime).toBeGreaterThanOrEqual(500) | ||
expect(await res.text()).toBe('john') | ||
}) | ||
|
||
test('uses realistic server response time when no duration is provided', async () => { | ||
server.use( | ||
rest.get('http://localhost/user', (req, res, ctx) => { | ||
return res(ctx.delay(), ctx.text('john')) | ||
}), | ||
) | ||
|
||
const { res, responseTime } = await makeRequest('http://localhost/user') | ||
|
||
// Realistic server response time in Node.js is set to 5ms. | ||
expect(responseTime).toBeGreaterThan(5) | ||
expect(responseTime).toBeLessThan(100) | ||
expect(await res.text()).toBe('john') | ||
}) | ||
|
||
test('uses realistic server response time when "real" mode is provided', async () => { | ||
server.use( | ||
rest.get('http://localhost/user', (req, res, ctx) => { | ||
return res(ctx.delay('real'), ctx.text('john')) | ||
}), | ||
) | ||
|
||
const { res, responseTime } = await makeRequest('http://localhost/user') | ||
|
||
// Realistic server response time in Node.js is set to 5ms. | ||
expect(responseTime).toBeGreaterThan(5) | ||
expect(await res.text()).toBe('john') | ||
}) |