Check out a free preview of the full Testing React Applications, v2 course:
The "Jest JSDOM" Lesson is part of the full, Testing React Applications, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kent shows that JSDOM is loaded by default in Jest. If not needed, the test environment can be configured to be Node instead.

Get Unlimited Access Now

Transcript from the "Jest JSDOM" Lesson

[00:00:00]
>> Kent C. Dodds: There is one thing that I am going to show you that doesn't actually apply specifically to react but is really useful for jest if you are writing JavaScript modules that are just pure JavaScript, don't need the DOM or anything like that. By default, jest actually loads JSDOM in the environment and here we can go in our utils, we can console.log(window).

[00:00:28] What, that's weird. And then we'll run our test again and we'll see the giant window object that's provided to us by JS DOM. Okay, so that's kind of fun and stuff. But if this were the only test that I had in my project, maybe this is a note project or it's just pure Java scripting, then I don't actually need the ability to access window.

[00:00:55] And so I don't need JS DOM. By adding JS DOM it actually is pretty heavy memory wise. And so it adds to the start time so that's why I'm showing you. Let's go ahead and add our configuration right in the [INAUDIBLE] JSON for jest. And here we can add a test environment and this is a path to a node module that is responsible for creating or setting up the test environment.

[00:01:22] And so there is a jest-environment-jsdom, and that's the default that's installed when you install jest so that's why this works. And then there's a jest environment node and that's also installed by default so you don't have to install that. There are various other test environments that are kind of interesting.

[00:01:42] I feel like there's even one that's like for selenium or web driver or something. It's just like what? It's crazy. Jest is a very, very versatile tool. Another handy feature about this test environment is if the module starts with jest-environment and you can actually remove that. So it reads a little nicer.

[00:02:01] So with that we can run this and now our test is going to fail because we're trying to log something that's not defined. So we are all set to have our tests run a little bit faster.