Questions tagged [jsdom]

Implementation of the WHATWG DOM and HTML Standards (among others) for node.js

jsdom allows construction of in-memory and in-process objects that represent the window, document, etc. from a browser. It tries to implement the latest DOM and HTML standards, among many others like the CSSOM.

820 questions
103
votes
9 answers

How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

I try to convert an existing project to use Typescript and I have problems doing so with my testing setup. I had a setup file for my tests that sets up jsdom so that all my DOM interacting code works during my tests. Using Typescript (ts-node with…
fahrradflucht
  • 1,563
  • 3
  • 14
  • 22
84
votes
9 answers

Mocking `document` in jest

I'm trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I'm facing an issue while loading the js file. I have followed a piece of code where document object has a currentScript object. But in test…
thecodejack
  • 12,689
  • 10
  • 44
  • 59
69
votes
7 answers

unit test raises error because of .getContext() is not implemented

I am writing tests using Jest for components that use canvas elements. I keep getting an error when I run my tests that looks like this. Error: Not implemented: HTMLCanvasElement.prototype.getContext (without installing the canvas npm package) From…
lpie88
  • 753
  • 1
  • 5
  • 5
69
votes
4 answers

error 'document' is not defined : eslint / React

I'm building a React app, with create-react-app. I got the following error when running ESLint: 8:3 error 'document' is not defined no-undef. My app runs without error, but I got this ESLint error in 2 files. See one of those .jsx files and my…
Thomas Sauvajon
  • 1,660
  • 2
  • 13
  • 26
66
votes
6 answers

How to manage a 'pool' of PhantomJS instances

I'm planning a webservice for my own use internally that takes one argument, a URL, and returns html representing the resolved DOM from that URL. By resolved I mean that the webservice will firstly get the page at that URL, then use PhantomJS to…
Trindaz
  • 17,029
  • 21
  • 82
  • 111
48
votes
3 answers

Does Jest reset the JSDOM document after every suite or test?

I'm testing a couple of components that reach outside of their DOM structure when mounting and unmounting to provide specific interaction capability that wouldn't be possible otherwise. I'm using Jest and the default JSDOM initialization to create a…
Klemen Slavič
  • 19,661
  • 3
  • 34
  • 43
43
votes
12 answers

How to add support to my tests in Jest?

In my Jest unit test I am rendering a component with a ColorPicker. The ColorPicker component creates a canvas object and 2d context but returns 'undefined' which throws an error "Cannot set property 'fillStyle' of undefined" if (typeof document ==…
a11hard
  • 1,904
  • 4
  • 19
  • 41
39
votes
5 answers

How to mock navigator.clipboard.writeText() in Jest?

I have tried the following 4 options after looking at Jest issues and SO answers, but I am either getting TypeScript errors or runtime errors. I would really like to get option 1 (spyOn) working. // ------ option 1 ----- // Gives this runtime error:…
Naresh
  • 23,937
  • 33
  • 132
  • 204
38
votes
4 answers

What are the use cases of jsdom

After reading this Micro templates are dead article. I've become curious: Whether Using the DOM on the server results in cleaner more maintainable code then templating. Whether it's more efficient to use jsdom instead of a templating engine. How to…
Raynos
  • 166,823
  • 56
  • 351
  • 396
38
votes
3 answers

mocha command giving ReferenceError: window is not defined

I am using command: mocha --compilers :./test/babel-setup.js --recursive --watch It is giving error: ReferenceError: window is not defined I have the following in my babel-setup.js: require("babel/register")({ compact: false }); I am using node…
jit
  • 1,616
  • 3
  • 21
  • 49
35
votes
3 answers

Scrape a webpage and navigate by clicking buttons

I want to perform following actions at the server side: 1) Scrape a webpage 2) Simulate a click on that page and then navigate to the new page. 3) Scrape the new page 4) Simulate some button clicks on the new page 5) Sending the data back to the…
user2129794
  • 2,388
  • 8
  • 33
  • 51
33
votes
4 answers

Jest / Enzyme - How to test at different viewports?

I am trying to run a test on a component at a certain viewport width. I am doing the following, but this doesn't seem to change it: test('Component should do something at a certain viewport width.', () => { global.innerWidth = 2000; const…
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
31
votes
4 answers

Why am I getting "TextEncoder is not defined" in Jest?

When testing a function that uses either the TextEncoder or the TextDecoder I get: ReferenceError: TextEncoder is not defined ReferenceError: TextDecoder is not defined I am using jsdom, so why is this not working?
leonheess
  • 16,068
  • 14
  • 77
  • 112
27
votes
2 answers

JSDOM in nodeJS: How do I get back the manipulated html?

I am trying to manipulate a remote HTML and return it manipulated. I decided to use JSDOM but cannot figure out how to get the manipulated HTML back. Any ideas? jsdom.env({ url: "http://www.cnn.com", scripts:…
Guy
  • 12,488
  • 16
  • 79
  • 119
24
votes
3 answers

Testing React portals with enzyme

So I'm having a hard time writing tests for a modal component using React fiber's portal. Because my modal mounts to a domNode on the root of the but because that domNode doesn't exist, the test fails. Some code to give, context:…
Fabio Antunes
  • 22,251
  • 15
  • 81
  • 96
1
2 3
54 55