{"id":13451889,"url":"https://github.com/simon360/jest-environment-jsdom-global","last_synced_at":"2025-04-14T10:44:17.082Z","repository":{"id":27649368,"uuid":"114767299","full_name":"simon360/jest-environment-jsdom-global","owner":"simon360","description":"A Jest environment that allows you to configure jsdom","archived":false,"fork":false,"pushed_at":"2023-01-08T16:51:13.000Z","size":523,"stargazers_count":113,"open_issues_count":2,"forks_count":13,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T19:39:58.780Z","etag":null,"topics":["jest","jest-environment","jsdom"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simon360.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-19T13:18:01.000Z","updated_at":"2025-02-14T00:54:08.000Z","dependencies_parsed_at":"2022-07-08T10:50:41.224Z","dependency_job_id":null,"html_url":"https://github.com/simon360/jest-environment-jsdom-global","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon360%2Fjest-environment-jsdom-global","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon360%2Fjest-environment-jsdom-global/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon360%2Fjest-environment-jsdom-global/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon360%2Fjest-environment-jsdom-global/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simon360","download_url":"https://codeload.github.com/simon360/jest-environment-jsdom-global/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248867797,"owners_count":21174750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jest","jest-environment","jsdom"],"created_at":"2024-07-31T07:01:06.031Z","updated_at":"2025-04-14T10:44:17.038Z","avatar_url":"https://github.com/simon360.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Jest environment for a globally-exposed JSDOM\n\n\u003e Similar to the standard [`jest-environment-jsdom`](https://github.com/facebook/jest/tree/main/packages/jest-environment-jsdom), but exposes `jsdom` so that you can reconfigure it from your test suites.\n\nFor more information, see [this discussion](https://github.com/facebook/jest/issues/5124) in the Jest repository.\n\nBefore installing, please check if you need this package, particularly in light of [changes in Jest 28](#jest-28-update-is-this-package-still-useful).\n\n## Installation and configuration\n\nInstall the package with `yarn`:\n\n```shell\nyarn add --dev jest-environment-jsdom-global jest-environment-jsdom\n```\n\nor `npm`:\n\n```shell\nnpm install --save-dev jest-environment-jsdom-global jest-environment-jsdom\n```\n\nThen, add it to your Jest configuration:\n\n```json\n\"jest\": {\n  \"testEnvironment\": \"jest-environment-jsdom-global\"\n}\n```\n\nFor more information, see the [Jest documentation on `testEnvironment`](https://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string).\n\n## Using JSDOM in your test suite\n\nYou can access the `jsdom` object globally in your test suite. For example, here's a test that changes the URL for your test environment (using [`reconfigure`](https://github.com/tmpvar/jsdom#reconfiguring-the-jsdom-with-reconfiguresettings)):\n\n```javascript\ndescribe(\"test suite\", () =\u003e {\n  it(\"should not fail\", () =\u003e {\n    jsdom.reconfigure({\n      url: \"https://www.example.com/\",\n    });\n  });\n});\n```\n\n## Frequently Asked Questions\n\n### Jest 28 update: is this package still useful?\n\n[As of Jest 28](https://jestjs.io/blog/2022/04/25/jest-28#inline-testenvironmentoptions) ([formal docs](https://jestjs.io/docs/configuration#testenvironmentoptions-object)), you can provide options to JSDOM using inline comments in your tests. For example, to set the URL, you could write a test suite that looks like this:\n\n```\n/**\n * @jest-environment jsdom\n * @jest-environment-options {\"url\": \"https://jestjs.io/\"}\n */\n\ntest('use jsdom and set the URL in this test file', () =\u003e {\n  expect(window.location.href).toBe('https://jestjs.io/');\n});\n```\n\nThis may solve for many previous use cases for this package, and I'd suggest using this approach rather than using `jest-environment-jsdom-global` if it suffices for your needs.\n\n### Why can't I use `Object.defineProperty`?\n\nJest's browser environment is based on JSDOM. JSDOM used to allow you to use `Object.defineProperty` to update certain properties on `window`; in particular, you could change parts of `window.location`, or `window.top`, as you need to.\n\nHowever, several years ago, JSDOM's API changed; the preferred way to mock `window.location` and its child properties is to use [`reconfigure`](https://github.com/tmpvar/jsdom#reconfiguring-the-jsdom-with-reconfiguresettings). JSDOM 11 became the default back in Jest 22; as a result, tests that used `Object.defineProperty` may no longer work on certain properties of `window`.\n\nCurrently, Jest does not expose the JSDOM `reconfigure` method inside test suites. The `jest-environment-jsdom-global` package is meant to solve this problem: it adds `jsdom` as a global, so you can reconfigure it within your tests.\n\n### How can I mock `window.location.href`?\n\nIn your test, you can set the URL using:\n\n```javascript\njsdom.reconfigure({\n  url: \"https://www.example.com/\",\n});\n```\n\n### How can I mock `window.location.hash`?\n\nYou need to provide a full URL, not just the hash. Similarly to above, you can do:\n\n```javascript\njsdom.reconfigure({\n  url: \"https://www.example.com/#myHash\",\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimon360%2Fjest-environment-jsdom-global","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimon360%2Fjest-environment-jsdom-global","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimon360%2Fjest-environment-jsdom-global/lists"}