All Projects → simon360 → Jest Environment Jsdom Global

simon360 / Jest Environment Jsdom Global

Licence: mit
A Jest environment that allows you to configure jsdom

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jest Environment Jsdom Global

Lwc Recipes
A collection of easy-to-digest code examples for Lightning Web Components on Salesforce Platform
Stars: ✭ 1,147 (+1188.76%)
Mutual labels:  jest
Mostly
They mostly come at night; mostly.
Stars: ✭ 78 (-12.36%)
Mutual labels:  jest
Fullstack Shopping Cart
MERN stack shopping cart, written in TypeScript
Stars: ✭ 82 (-7.87%)
Mutual labels:  jest
Eslint Plugin Jest Formatting
ESLint rules for formatting test suites written for jest.
Stars: ✭ 71 (-20.22%)
Mutual labels:  jest
Openapivalidators
Use Jest or Chai to assert that HTTP responses satisfy an OpenAPI spec
Stars: ✭ 77 (-13.48%)
Mutual labels:  jest
Babel Test
An opinionated library to make testing babel plugins easier.
Stars: ✭ 79 (-11.24%)
Mutual labels:  jest
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+1188.76%)
Mutual labels:  jest
Codecharta
CodeCharta visualizes multiple code metrics using 3D tree maps.
Stars: ✭ 85 (-4.49%)
Mutual labels:  jest
Gatsby Themes
Get high-quality and customizable Gatsby themes to quickly bootstrap your website! Choose from many professionally created and impressive designs with a wide variety of features and customization options.
Stars: ✭ 1,208 (+1257.3%)
Mutual labels:  jest
Dockest
Docker + Jest integration testing for Node.js
Stars: ✭ 81 (-8.99%)
Mutual labels:  jest
Karmatic
🦑 Easy automatic (headless) browser testing with Jest's API, but powered by Karma & Webpack.
Stars: ✭ 1,178 (+1223.6%)
Mutual labels:  jest
Jest Action
Wraps and install Jest test runner in a github action.
Stars: ✭ 74 (-16.85%)
Mutual labels:  jest
Hacker News Resolve
React & Redux & Resolve implementation of Hacker News
Stars: ✭ 79 (-11.24%)
Mutual labels:  jest
Generator Rn Toolbox
The React Native Generator to bootstrap your apps
Stars: ✭ 1,155 (+1197.75%)
Mutual labels:  jest
Reactjs Crud Boilerplate
Live Demo
Stars: ✭ 83 (-6.74%)
Mutual labels:  jest
React Native Template Typescript
👾 Clean and minimalist React Native template for a quick start with TypeScript.
Stars: ✭ 1,148 (+1189.89%)
Mutual labels:  jest
Gatsby Starter Typescript Rebass Netlifycms
My default Gatsby setup. Includes rich MDX support.
Stars: ✭ 79 (-11.24%)
Mutual labels:  jest
React Boilerplate
This project is deprecated. Please use CRA instead.
Stars: ✭ 88 (-1.12%)
Mutual labels:  jest
Jest Marbles
Helpers library for marbles testing with Jest
Stars: ✭ 85 (-4.49%)
Mutual labels:  jest
Alias Hq
The end-to-end solution for configuring, refactoring, maintaining and using path aliases
Stars: ✭ 77 (-13.48%)
Mutual labels:  jest

Jest environment for a globally-exposed JSDOM

Similar to the standard jest-environment-jsdom, but exposes jsdom so that you can reconfigure it from your test suites.

For more information, see this discussion in the Jest repository.

Installation and configuration

Install the package with yarn:

yarn add --dev jest-environment-jsdom-global jest-environment-jsdom

or npm:

npm install --save-dev jest-environment-jsdom-global jest-environment-jsdom

Then, add it to your Jest configuration:

"jest": {
  "testEnvironment": "jest-environment-jsdom-global"
}

For more information, see the Jest documentation on testEnvironment.

Using JSDOM in your test suite

You 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):

describe("test suite", () => {
  it("should not fail", () => {
    jsdom.reconfigure({
      url: "https://www.example.com/",
    });
  });
});

Using JSDOM 16 (Jest 25 and lower)

NOTE: Jest 26 uses JSDOM 16 out of the box. These instructions only apply for versions of Jest < 26.0.0.

If you want to swap jest-environment-jsdom for jest-environment-jsdom-sixteen, simply install it.

When jest-environment-jsdom-global is able to find the jest-environment-jsdom-sixteen package, that package will be used instead of jest-environment-jsdom.

Frequently Asked Questions

Why can't I use Object.defineProperty?

Jest'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.

However, in recent versions, JSDOM's API has changed; the preferred way to mock window.location and its child properties is to use reconfigure. JSDOM 11 became the default in Jest 22 (JSDOM 15 as of Jest 25); as a result, tests that used Object.defineProperty may no longer work on certain properties of window.

Currently, 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.

How can I mock window.location.href?

In your test, you can set the URL using:

jsdom.reconfigure({
  url: "https://www.example.com/",
});

How can I mock window.location.hash?

You need to provide a full URL, not just the hash. Similarly to above, you can do:

jsdom.reconfigure({
  url: "https://www.example.com/#myHash",
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].