Gathering detailed insights and metrics for xhr-mocklet
Gathering detailed insights and metrics for xhr-mocklet
Gathering detailed insights and metrics for xhr-mocklet
Gathering detailed insights and metrics for xhr-mocklet
npm install xhr-mocklet
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
70 Commits
1 Forks
4 Watching
5 Branches
9 Contributors
Updated on 04 Jan 2020
TypeScript (97.17%)
Shell (2.83%)
Cumulative downloads
Total Downloads
Last day
66.7%
15,379
Compared to previous day
Last week
25.7%
71,821
Compared to previous week
Last month
102.6%
277,358
Compared to previous month
Last year
40.1%
2,002,414
Compared to previous year
Utility for mocking XMLHttpRequests both in the browser and nodejs. It's primary use case is for unit tests, allowing you to respond with mock responses, trigger timeouts, etc.
This library comes with complete TypeScript declaration files.
1# NPM 2npm install --save --dev xhr-mocklet 3 4# Or via yarn: 5yarn add --dev xhr-mocklet
1const mock = require('xhr-mocklet'); 2 3// Replace the real XHR object with the mock XHR object 4mock.setup(); 5 6// Mock a response for all POST requests to http://localhost/api/user 7mock.post('http://localhost/api/user', (req, res) => { 8 return res 9 .status(201) 10 .header('Content-Type', 'application/json') 11 .body({ 12 lastName: 'John', 13 firstName: 'Smith' 14 }); 15}); 16 17// Restore the original XHR object when all your tests are done. 18mock.teardown();
Simply return null
from your response handler:
1mock.post('http://localhost/foo', (req, res) => null);
1mock.post('http://localhost/foo', (req, res) => res.timeout(true));
XMLHttpRequest
You can even use a mocked XMLHttpReqeuest
instance to create Requests:
1// Create an instance of the (mock) XHR object and use as per normal 2const xhr = new XMLHttpRequest(); 3 4xhr.onreadystatechange = () => { 5 if (xhr.readyState === xhr.DONE) { 6 // when you're finished put the real XHR object back 7 mock.teardown(); 8 } 9}
Method | Description |
---|---|
setup() | Replace the global XMLHttpRequest object with the MockXMLHttpRequest . |
teardown() | Restore the global XMLHttpRequest object to its original state. |
reset() | Remove all request handlers. |
get(url: string | regex, callback) | Create GET mock response for a specific url. |
post(url: string | regex, callback) | Create POST mock response for a specific url. |
put(url: string | regex, callback) | Create PUT mock response for a specific url. |
patch(url: string | regex, callback) | Create PATCH mock response for a specific url. |
delete(url: string | regex, callback) | Create DELETE mock response for a specific url. |
mock(callback) | Register mock response for every request. |
The api is practically similar to the native XMLHttpRequest.
Method | Description |
---|---|
method(): string | Get the request method. |
url(): string | Get the request URL. |
query(): object | Get the parsed query part of the request URL. |
header(name: string): string | Get a request header. |
headers(): object | Get all request headers. |
body(): string | Get the request body. |
Method | Description |
---|---|
status(): number | Get the response status. |
status(code: number) | Set the response status. |
header(name: string): string | Get a response header. |
header(name: string, value: string) | Set a response header. |
headers(): object | Get response headers. |
headers(headers: obejct) | Set response headers. |
body(): string | Get response body. |
body(body: string) | Set response body. |
timeout(): boolean | number | Get weather the response will trigger a timeout. |
timeout(ms: boolean | number) | Set a timeout, otherwise default to the value set on the XHR object. |
progress(loaded: number, total: number, lengthComputable: boolean) | Trigger progress event. Pass in loaded size, total size and if event is lengthComputable. |
Special thanks goes to James Newell for his
xhr-mock library. xhr-mocklet
started out as a fork of his work.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 7/18 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
38 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More