TypeError and Access Issues in jsdom Environment Post Update to undici 6.16.0
KieraDOG opened this issue · 4 comments
Bug Description
After updating undici
from version 6.15.0 to 6.16.0, our jsdom
testing environment started throwing errors related to markResourceTiming
and accessing the location
property on Window
. These issues are causing our unit tests, which utilize msw
for mocking network requests, to fail. The errors suggest a problem with how undici
interacts with the global properties in jsdom
.
Reproducible By
- Update
undici
from version 6.15.0 to 6.16.0. - Set up a
jsdom
environment to run unit tests. - Configure
msw
to handle network requests in the test environment. - Execute the tests that involve network requests using
undici
.
Expected Behavior
Tests should pass without throwing errors related to markResourceTiming
or issues accessing the location
property on Window
. The update from undici
6.15.0 to 6.16.0 should not affect the functionality of existing tests in a jsdom
environment.
Logs & Screenshots
The errors include:
TypeError: markResourceTiming is not a function
TypeError: Cannot read properties of null (reading 'location')
Environment
- OS: Linux
- Node.js Version: 20.9.0
undici
Version: 6.16.0jest-environment-jsdom
Version: 29.7.0msw
Version: 2.3.0
Additional context
The issue occurred directly after updating undici
to the latest version, and reverting to the previous version resolves the errors. This suggests a compatibility issue introduced in the latest release.
Do you have a minimal reproducible example?
These kind of things happens because both msw and jest heavily monkeypatch globals. It's very hard for us to keep track of all those things.
test("fetch - github", async () => {
globalThis.TextEncoder = require("node:util").TextEncoder;
globalThis.TextDecoder = require("node:util").TextDecoder;
globalThis.ReadableStream = require("node:stream/web").ReadableStream;
globalThis.WritableStream = require("node:stream/web").WritableStream;
globalThis.CompressionStream = require("node:stream/web").CompressionStream;
globalThis.DecompressionStream = require("node:stream/web").DecompressionStream;
globalThis.setImmediate = require("node:timers").setImmediate;
globalThis.clearImmediate = require("node:timers").clearImmediate;
globalThis.crypto = require("node:crypto").webcrypto;
// just adding this
globalThis.performance.markResourceTiming = () => {};
const { fetch } = require("undici");
await fetch("https://github.com").then(c => c.text());
});
These kind of things happens because both msw and jest heavily monkeypatch globals
I will play devil's advocate here a bit and mention that MSW has no issues running Undici. We are running our entire test suite against Undici in Vitest and haven't had any issues whatsoever. Every issue you are experiencing is caused by Jest and underlying JSDOM (sometimes, jest-environment-jsdom
as well). Try Vitest and HappyDOM, you'll likely have most of those issues fixed automatically.