Skip to content

Commit

Permalink
test: improve integration tests (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Feb 26, 2022
1 parent 77ce97a commit 4563ce7
Show file tree
Hide file tree
Showing 31 changed files with 1,384 additions and 1,168 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -58,7 +58,8 @@
"superagent": "^6.1.0",
"supertest": "^6.1.6",
"ts-jest": "^27.1.1",
"typescript": "4.3.5"
"typescript": "4.3.5",
"wait-for-expect": "^3.0.2"
},
"dependencies": {
"@open-draft/until": "^1.0.3",
Expand Down
2 changes: 2 additions & 0 deletions src/interceptors/ClientRequest/NodeClientRequest.ts
Expand Up @@ -147,6 +147,7 @@ export class NodeClientRequest extends ClientRequest {

callback?.()

this.log('emitting the custom "response" event...')
this.observer.emit('response', isomorphicRequest, isomorphicResponse)

return
Expand Down Expand Up @@ -189,6 +190,7 @@ export class NodeClientRequest extends ClientRequest {
this.log(response.statusCode, response.statusMessage, responseBody)
this.log('original response headers:', response.headers)

this.log('emitting the custom "response" event...')
this.observer.emit('response', isomorphicRequest, {
status: response.statusCode || 200,
statusText: response.statusMessage || 'OK',
Expand Down
93 changes: 51 additions & 42 deletions test/features/events/request.test.ts
Expand Up @@ -3,21 +3,28 @@
*/
import * as http from 'http'
import { createServer, ServerApi } from '@open-draft/test-server'
import { createInterceptor, IsomorphicRequest } from '../../../src'
import {
createInterceptor,
InterceptorEventsMap,
IsomorphicRequest,
} from '../../../src'
import { interceptXMLHttpRequest } from '../../../src/interceptors/XMLHttpRequest'
import { interceptClientRequest } from '../../../src/interceptors/ClientRequest'
import { createXMLHttpRequest, httpRequest } from '../../helpers'
import { createXMLHttpRequest, waitForClientRequest } from '../../helpers'
import { anyUuid, headersContaining } from '../../jest.expect'

let requests: IsomorphicRequest[] = []
let httpServer: ServerApi

const interceptor = createInterceptor({
modules: [interceptClientRequest, interceptXMLHttpRequest],
resolver() {},
})

interceptor.on('request', (request) => {
requests.push(request)
})
const requestListener = jest.fn<
ReturnType<InterceptorEventsMap['request']>,
Parameters<InterceptorEventsMap['request']>
>()
interceptor.on('request', requestListener)

beforeAll(async () => {
httpServer = await createServer((app) => {
Expand All @@ -30,43 +37,45 @@ beforeAll(async () => {
})

afterEach(() => {
requests = []
jest.resetAllMocks()
})

afterAll(async () => {
interceptor.restore()
await httpServer.close()
})

test('ClientRequest: emits the "request" event upon the request', (done) => {
const request = http.request(
httpServer.http.makeUrl('/user'),
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
test('ClientRequest: emits the "request" event upon the request', async () => {
const url = httpServer.http.makeUrl('/user')
const req = http.request(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
() => {
expect(requests).toHaveLength(1)
const [request] = requests
})
req.write(JSON.stringify({ userId: 'abc-123' }))
req.end()
await waitForClientRequest(req)

expect(request.method).toEqual('POST')
expect(request.url.href).toEqual(httpServer.http.makeUrl('/user'))
expect(request.headers.get('content-type')).toEqual('application/json')
expect(request.body).toEqual(JSON.stringify({ userId: 'abc-123' }))
done()
}
)
request.write(JSON.stringify({ userId: 'abc-123' }))
request.end()
expect(requestListener).toHaveBeenCalledTimes(1)
expect(requestListener).toHaveBeenCalledWith<[IsomorphicRequest]>({
id: anyUuid(),
method: 'POST',
url: new URL(url),
headers: headersContaining({
'content-type': 'application/json',
}),
credentials: expect.anything(),
body: JSON.stringify({ userId: 'abc-123' }),
})
})

test('XMLHttpRequest: emits the "request" event upon the request', async () => {
await createXMLHttpRequest((request) => {
request.open('POST', httpServer.http.makeUrl('/user'))
request.setRequestHeader('Content-Type', 'application/json')
request.send(JSON.stringify({ userId: 'abc-123' }))
const url = httpServer.http.makeUrl('/user')
await createXMLHttpRequest((req) => {
req.open('POST', url)
req.setRequestHeader('Content-Type', 'application/json')
req.send(JSON.stringify({ userId: 'abc-123' }))
})

/**
Expand All @@ -75,15 +84,15 @@ test('XMLHttpRequest: emits the "request" event upon the request', async () => {
* emitting the "request" event.
* @see https://github.com/mswjs/interceptors/issues/163
*/
expect(requests).toHaveLength(4)

const [request] = requests
expect(request.method).toEqual('POST')
expect(request.url.href).toEqual(httpServer.http.makeUrl('/user'))
expect(request.headers.get('content-type')).toEqual('application/json')
expect(request.body).toEqual(
JSON.stringify({
userId: 'abc-123',
})
)
expect(requestListener).toHaveBeenCalledTimes(2)
expect(requestListener).toHaveBeenCalledWith<[IsomorphicRequest]>({
id: anyUuid(),
method: 'POST',
url: new URL(url),
headers: headersContaining({
'content-type': 'application/json',
}),
credentials: 'omit',
body: JSON.stringify({ userId: 'abc-123' }),
})
})
144 changes: 0 additions & 144 deletions test/features/events/response.node.test.ts

This file was deleted.

0 comments on commit 4563ce7

Please sign in to comment.