Comments (9)
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.
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.
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.
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.
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.
@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 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.
hmm... not sure, looks just like mine. (additionally i have this.global.FormData = FormData;
, but that doesnt seem like ur issue here)
from ky.
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)
- Cannot use import statement outside a module, Jest HOT 1
- Types for headers HOT 2
- How to handle HTTP Delete Method (204) with ky?
- [BUG] When Response is a string(not valid JSON), `json` method occurs error
- Cannot find module 'undici-types' or its corresponding type declarations
- ky response is an empty string sometimes HOT 2
- response.clone(...).arrayBuffer is not a function
- Content-Type detection HOT 3
- In Next.js app: Only absolute URLs are supported HOT 1
- Compatibility issue with Cloudflare Worker HOT 2
- FormData & Files Encoding
- Why I keep getting HTTPError when the throwHttpErrors is already set to `false` HOT 2
- Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'headers') HOT 1
- beforeError hook isn't called on 500 responses HOT 2
- how to send binary data? HOT 1
- Error: unsupported BodyInit type
- retry not working? HOT 1
- HTTPError json as unknown/type
- extend: append to prefixUrl HOT 3
- Refresh token with ky: 2 calls instead of 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ky.