XMLHttpRequest mock module for Jest testing framework
xmlhttprequest-jest-mock
is a mocking module for native browser XMLHttpRequest
function.
- custom data inject for testing request responses
- full control over request functions and properties
- mocked native functions to spy on inner functions/properties of request
# install dependencies
npm install
# start dev test server
npm test:unit:Dev
# start dev build with watch option
npm build:Dev
npm i xmlhttprequest-jest-mock
Please refer to __tests__/example/example.test.js
for a sample usage.
- type:
Function
- returns:
mockObject
- description: This is the main function that will be returned as a result of your require/import statement. Executing it will setup necessary steps to mock native
XMLHttpRequest
. ReturnedmockObject
is key for further testing.
- type:
object
- props:
setData
: sets data to return on request- type:
Function
- arguments:
newData
: data to be return on request- type:
any
- type:
newStatus
: new status for response- type:
number
- default:
200
- type:
newReadyState
: new readystate code for response- type:
number
- default:
4
- type:
- type:
cleanUp
: clean up mocked implementation- type:
Function
- type:
- [
open
,onreadystatechange
,addEventListener
,send
]: spyed on native functions ofXMLHttpRequest
- types:
Function
- types:
- description: Object with various methods for testing.
Please fell free to contact for any updates/bugs.
Erdem Bircan (c) 2019-present