Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError and Access Issues in jsdom Environment Post Update to undici 6.16.0 #3237

Closed
KieraDOG opened this issue May 10, 2024 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@KieraDOG
Copy link

KieraDOG commented May 10, 2024

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

  1. Update undici from version 6.15.0 to 6.16.0.
  2. Set up a jsdom environment to run unit tests.
  3. Configure msw to handle network requests in the test environment.
  4. 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

image

image

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.0
  • jest-environment-jsdom Version: 29.7.0
  • msw 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.

@KieraDOG KieraDOG added the bug Something isn't working label May 10, 2024
@KhafraDev
Copy link
Member

Do you have a minimal reproducible example?

@mcollina
Copy link
Member

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.

@tsctx
Copy link
Member

tsctx commented May 11, 2024

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());
});

@kettanaito
Copy link
Contributor

kettanaito commented May 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants