mock-xmlhttprequest
Version:
XMLHttpRequest mock for testing
40 lines (36 loc) • 1.24 kB
JavaScript
/**
* mock-xmlhttprequest v8.4.1
* (c) 2025 Bertrand Guay-Paquet
* @license MIT
*/
import MockXhr from './MockXhr.mjs';
import MockXhrServer from './MockXhrServer.mjs';
/**
* 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);
}
export { newMockXhr, newServer };