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

[Bug]: URLSearchParams#get(k, v) is not implemented and uses URLSearchParams#get(k) instead #14954

Closed
kvndrsslr opened this issue Mar 11, 2024 · 2 comments

Comments

@kvndrsslr
Copy link

Version

29.7.0

Steps to reproduce

Prepare

git clone https://github.com/kvndrsslr/jest-bug-urlsearchparams-has-k-v.git
cd jest-bug-urlsearchparams-has-k-v
npm ci

Test

npm run test

This fails only on ./jsdom-test-env.test.js.
Now try to comment out line 3 in ./jest.config.js and run again:

npm run test

This time all the tests pass.

Expected behavior

URLSearchParams.has(k,v) works as expected.

Actual behavior

Instead of URLSearchParams.has(k,v), only URLSearchParams.has(k) is implemented and hence returns false positives in tests that contain .has(k,v)

Additional context

URLSearchParams#has(k,v) seems to be not correctly implemented in jest only when using testEnvironment: 'jsdom'.
I have no idea where this goes wrong, as it works in node by default and it works when testing jsdom and whatwg-url in isolation too.

Environment

System:
    OS: macOS 14.4
    CPU: (12) arm64 Apple M2 Pro
  Binaries:
    Node: 18.19.1 - ~/.volta/tools/image/node/18.19.1/bin/node
    npm: 10.2.4 - ~/.volta/tools/image/node/18.19.1/bin/npm
    pnpm: 8.15.0 - ~/.volta/bin/pnpm
    bun: 1.0.30 - /opt/homebrew/bin/bun
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@SimenB
Copy link
Member

SimenB commented Mar 11, 2024

That's because you're using jsdom@24 in your test, which is the newest version. If you install jsdom@20 (which is what ships with Jest) you can see the test failing in the same way with the builtin test runner.

jest-environment-jsdom@next (v30 alpha) ships with jsdom@22. You can setup a version with jsdom@24 using https://www.npmjs.com/package/@jest/environment-jsdom-abstract

diff --git c/custom-jsdom-env.js w/custom-jsdom-env.js
new file mode 100644
index 0000000..4b132f6
--- /dev/null
+++ w/custom-jsdom-env.js
@@ -0,0 +1,7 @@
+const { default: TestEnvironment } = require('@jest/environment-jsdom-abstract');
+
+module.exports = class JSDOMEnv extends TestEnvironment {
+  constructor(config, context) {
+    super(config, context, require('jsdom'));
+  }
+}
diff --git c/jest.config.js w/jest.config.js
index 69f6556..7d1219c 100644
--- c/jest.config.js
+++ w/jest.config.js
@@ -1,6 +1,6 @@
 module.exports = {
   // comment out this line and the test is successful
-  testEnvironment: 'jsdom',
+  testEnvironment: './custom-jsdom-env.js',
   collectCoverage: false,
   moduleFileExtensions: ['js'],
   resetMocks: true,
diff --git c/package.json w/package.json
index 2219366..fe9e848 100644
--- c/package.json
+++ w/package.json
@@ -10,8 +10,8 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "@jest/environment-jsdom-abstract": "^30.0.0-alpha.3",
     "jest": "^29.7.0",
-    "jest-environment-jsdom": "^29.7.0",
     "jsdom": "^24.0.0",
     "whatwg-url": "^14.0.0"
   },

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants