-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
59 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { assert } from 'node:assert'; | ||
import { newServer } from 'mock-xmlhttprequest'; | ||
|
||
function functionToTest() { | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open('GET', '/my/url'); | ||
xhr.onload = () => resolve(JSON.parse(xhr.response)); | ||
xhr.onerror = () => reject(xhr.statusText); | ||
xhr.send(); | ||
}); | ||
} | ||
|
||
const server = newServer({ | ||
get: ['/my/url', { | ||
// status: 200 is the default | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: '{ "message": "Success!" }', | ||
}], | ||
}); | ||
|
||
async function integrationTest() { | ||
try { | ||
server.install(); | ||
const result = await functionToTest(); | ||
assert.equal(result.message, 'Success!'); | ||
} finally { | ||
// Restore the original XMLHttpRequest | ||
server.remove(); | ||
} | ||
} | ||
|
||
integrationTest(); |
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,11 @@ | ||
{ | ||
"name": "mock-xmlhttprequest-integration-test-esm", | ||
"description": "Integration test for XMLHttpRequest in a Node.js module project", | ||
"type": "module", | ||
"scripts": { | ||
"test": "node index.js" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
} | ||
} |