Skip to content

Commit

Permalink
fix: set readyState to 4 on response
Browse files Browse the repository at this point in the history
As for the mocked request also bypass request should have the readyState set to 4 (DONE) when a response is received

fix: mswjs/msw#636
  • Loading branch information
marcosvega91 committed Mar 9, 2021
1 parent 0db6518 commit 58cda4f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/interceptors/XMLHttpRequest/XMLHttpRequestOverride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export const createXMLHttpRequestOverride = (
this.response = originalRequest.response
this.responseText = originalRequest.responseText
this.responseXML = originalRequest.responseXML
this.readyState = this.DONE

debug(
'received original response status:',
Expand Down
38 changes: 36 additions & 2 deletions test/response/axios.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import axios from 'axios'
import { ServerApi, createServer } from '@open-draft/test-server'
import { createInterceptor } from '../../src'
import nodeInterceptors from '../../src/presets/node'

let server: ServerApi

const interceptor = createInterceptor({
modules: nodeInterceptors,
resolver(request) {
Expand All @@ -20,12 +23,27 @@ const interceptor = createInterceptor({
},
})

beforeAll(() => {
beforeAll(async () => {
server = await createServer((app) => {
app.get('/books', (req, res) => {
res.status(200).json([
{
title: 'The Lord of the Rings',
author: 'J. R. R. Tolkien',
},
{
title: 'The Hobbit',
author: 'J. R. R. Tolkien',
},
])
})
})
interceptor.apply()
})

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

test('responds with a mocked response to an "axios()" request', async () => {
Expand All @@ -51,3 +69,19 @@ test('responds with a mocked response to an "axios.post()" request', async () =>
expect(res.headers).toHaveProperty('x-header', 'yes')
expect(res.data).toEqual({ mocked: true })
})

test('bypass the interceptor and return the original response', async () => {
const res = await axios.get(server.http.makeUrl('/books'))

expect(res.status).toEqual(200)
expect(res.data).toEqual([
{
title: 'The Lord of the Rings',
author: 'J. R. R. Tolkien',
},
{
title: 'The Hobbit',
author: 'J. R. R. Tolkien',
},
])
})

0 comments on commit 58cda4f

Please sign in to comment.