Prevent external assets from being fetched? #1819
Replies: 4 comments
-
|
Hi @stevenvachon! I believe that the errors are thrown when the window is closed during tear-down, as it will then abort all ongoing requests. You can set the setting errorCapture to "processLevel" to improve error capturing and contain errors within the Window context during tear-down, however this setting doesn't work in test environments such as Vitest or Jest, as it collides with their process level error capturing. If the errors are captured already, you may want to consider disabling the console during tear down, perhaps by using the virtual console and stop listening for the "print" event. To prevent certain requests from being fetched, you can use the setting fetch.interceptor, which makes it possible to intercept a fetch request and return a custom response. import { Window } from "happy-dom";
const window = new Window({
settings: {
fetch: : {
interceptor: {
beforeAsyncRequest: async ({ request, window }) => {
if (request.url === "https://website/file") {
return new window.Response("");
}
}
}
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
|
Hi, @capricorn86, thank you for the response! I tried creating two test helpers out of the code you'd provided, but it doesn't appear to be used at all with my test.js: beforeAll(() => suppressBackgroundRequests());
it('renders', () => render(<iframe src="http://anything" />));helpers.js: import { Window } from 'happy-dom';
const ORIGINAL_WINDOW = globalThis.window;
/**
* @param {string[]} [urlStartsWith]
*/
export const suppressBackgroundRequests = (urlStartsWith) => {
globalThis.window = new Window({
settings: {
fetch: {
interceptor: {
beforeAsyncRequest: async ({ request, window }) => {
if (
!urlStartsWith ||
urlStartsWith.some((url) => request.url.startsWith(url))
) {
console.log('SUPPRESSION TEST');
return new window.Response('REQUEST SUPPRESSED');
}
},
},
},
},
});
};
export const unsuppressBackgroundRequests = () => {
globalThis.window = ORIGINAL_WINDOW;
};I still see "DOMException [NetworkError]" in the test logs and I never see "SUPPRESSION TEST". |
Beta Was this translation helpful? Give feedback.
-
|
For now I'm using this: export default () =>
Object.defineProperty(HTMLIFrameElement.prototype, 'src', {
configurable: true,
get() {},
set(value) {},
}); |
Beta Was this translation helpful? Give feedback.
-
|
@stevenvachon If you are using a test environment like Vitest or Jest, you need to set the options in the config for the environment. Another way is to set the setting Example for Vitest: export default defineConfig({
test: {
environment: 'happy-dom',
environmentOptions: {
happyDOM: {
settings: {
navigation: {
disableChildPageNavigation: true,
disableChildFrameNavigation: true
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm replacing the document with HTML that contains external asses such as
<link href>and<script src>and I'd like to prevent them from being fetched as to avoid seeing this error in my test logs:I tried stubbing the global
fetch, but it affects nothing, which makes sense.Beta Was this translation helpful? Give feedback.
All reactions