Giter Site home page Giter Site logo

Comments (9)

sholladay avatar sholladay commented on May 25, 2024 1

I would recommend just running your tests in an actual browser with something like Playwright, rather than trying to emulate one with something like JSDOM.

JSDOM was designed to facilitate simple DOM based unit tests, nothing more. And it made sense at the time because browser automation tools barely existed and were cumbersome. But that's not the case anymore.

Check out how Ky's own tests are implemented. We do run many tests in Node, because they don't necessarily need a DOM or web API polyfills, especially in recent versions of Node. But for anything complicated, or where the test was really critical, I would put it in our browser tests. The boilerplate and performance overhead is minimal.

from ky.

sindresorhus avatar sindresorhus commented on May 25, 2024

Sounds like you're running it in an older Node.js version than you think you are. throwIfAborted is definitely available on Node.js 18.

from ky.

darrinmn9 avatar darrinmn9 commented on May 25, 2024

hmm 🤔 , in the jest output right after this error stack trace it says Node.js v18.19.0 (jest version 29.7.0). This also happens in both my local environment and CI (where i am confident only 1 version of node.js is ever installed). Maybe i can try different jest versions and see if maybe its an issue/regression related to my specific jest testing environment.

from ky.

darrinmn9 avatar darrinmn9 commented on May 25, 2024

ahhh silly me, I should have suspected the issue was with the jsdom environment. (jsdom doesnt currently support the global Fetch api).

Reference to issue and suggested workarounds for jsdom fetch support for others who may come across this:
jsdom/jsdom#1724

from ky.

marc-at-brightnight avatar marc-at-brightnight commented on May 25, 2024

ahhh silly me, I should have suspected the issue was with the jsdom environment. (jsdom doesnt currently support the global Fetch api).

Reference to issue and suggested workarounds for jsdom fetch support for others who may come across this: jsdom/jsdom#1724

anyone still have an issue even after implementing this? I added the AbortController as well, as suggested by @darrinmn9 but no luck. I still get the error when running tests.

This is blocking me from upgrading my ky to v1, any help is appreciated.

Using node 18.12.1. Below is my jest.config.ts

import type { Config } from 'jest';

const config: Config = {
  testEnvironment: './FixJSDOMEnvironment.ts',
};

export default config;

from ky.

darrinmn9 avatar darrinmn9 commented on May 25, 2024

@marc-at-brightnight what is the exact error + stack trace you are getting, and what does ur FixJSDOMEnvironment.ts file look like?

from ky.

marc-at-brightnight avatar marc-at-brightnight commented on May 25, 2024

@marc-at-brightnight what is the exact error + stack trace you are getting, and what does ur FixJSDOMEnvironment.ts file look like?

----stack trace-----
/Users/mnhmbp/PycharmProjects/my-app/node_modules/ky/distribution/utils/delay.js:13
      signal.throwIfAborted();

TypeError: signal.throwIfAborted is not a function
    at throwIfAborted (/Users/mnhmbp/PycharmProjects/my-app/node_modules/ky/source/utils/delay.ts:15:11)
    at new Promise (<anonymous>)
    at delay (/Users/mnhmbp/PycharmProjects/my-app/node_modules/ky/source/utils/delay.ts:13:9)
    at Ky._retry (/Users/mnhmbp/PycharmProjects/my-app/node_modules/ky/source/core/Ky.ts:259:16)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Promise.result.<computed> [as json] (/Users/mnhmbp/PycharmProjects/my-app/node_modules/ky/source/core/Ky.ts:86:27)
// FixJSDOMEnvironment.ts

import JSDOMEnvironment from 'jest-environment-jsdom';

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
  constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
    super(...args);

    // FIXME https://github.com/jsdom/jsdom/issues/1724
    this.global.fetch = fetch;
    this.global.Headers = Headers;
    this.global.Request = Request;
    this.global.Response = Response;
    this.global.AbortController = AbortController;
  }
}
// package.json

"dependencies: {
  "ky": "^1.1.3",
  "jest-environment-jsdom": "^29.7.0"
  ...
}

Appreciate you taking a look!

from ky.

darrinmn9 avatar darrinmn9 commented on May 25, 2024

hmm... not sure, looks just like mine. (additionally i have this.global.FormData = FormData;, but that doesnt seem like ur issue here)

from ky.

darrinmn9 avatar darrinmn9 commented on May 25, 2024

totally agree! I am just trying to upgrade a large, existing jest test suite. But yes the longterm goal is to get off JSDOM... because as you've mentioned we have better solutions in 2024.

from ky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.