mock-xmlhttprequest
Version:
XMLHttpRequest mock for testing
45 lines (39 loc) • 1.37 kB
JavaScript
/**
* mock-xmlhttprequest v8.4.1
* (c) 2025 Bertrand Guay-Paquet
* @license MIT
*/
;
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const MockXhr = require('./MockXhr.cjs');
const MockXhrServer = require('./MockXhrServer.cjs');
/**
* Create a new "local" MockXhr subclass. Using a subclass of `MockXhr` in each test case makes it
* easier to ensure they are self-contained. For example if you set the onSend static property on
* a subclass, this will only affect that subclass and not the others created in your other test
* cases. You therefore don't need to add cleanup code to revert the changes made to the subclass.
*
* @returns New MockXhr subclass
*/
function newMockXhr() {
var _a;
return _a = class LocalMockXhr extends MockXhr {
constructor() {
super();
// Call the local MockXhr subclass' onCreate hook on the new mock instance
_a.onCreate?.(this);
}
},
// Reset to default value to override the parent class' flag
_a.timeoutEnabled = true,
_a;
}
/**
* @param routes Routes
* @returns new MockXhrServerserver with its own MockXhr subclass.
*/
function newServer(routes) {
return new MockXhrServer(newMockXhr(), routes);
}
exports.newMockXhr = newMockXhr;
exports.newServer = newServer;