Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Jest be used as browser based testing tool?

Tags:

jestjs

I understand that Jest is a unit testing tool for developers used for JavaScript. Is Jest a browser based testing tool similar to Selenium or a functional testing tool?

like image 216
gtlkanth Avatar asked Jul 18 '17 15:07

gtlkanth


People also ask

Can Jest be used for browser testing?

Yes, you can use Jest Preview (https://github.com/nvh95/jest-preview) to debug your Jest test in a browser like Google Chrome. You don't have to debug a long HTML text when using Jest Preview anymore.

Does Jest require browser?

The answer is down to Jest not being a real browser. You have access to all of the various DOM APIs when writing Jest tests but these are provided by JSDOM and your tests are actually running in Node. Jest has no native way to run your tests in a browser environment out of the box.

What is Jest tool used for?

Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly. Jest is well-documented, requires little configuration and can be extended to match your requirements.


2 Answers

As you mention, Jest is meant to be a unit testing tool. Normally you'd write small tests for parts/components of a web-page. I'm not exactly sure what you mean by "Is Jest can be used as Browser based Testing tool?" but I've found there are two relevant areas where Jest can come into contact with browser based testing

  1. You can use a virtual DOM (like JSDOM) to render your components and test them in an environment similar to a browser. These are still unit tests but you'll have access to window and document and can test things like document click, window navigation, focused element etc.

  2. You can debug your Jest tests in browser. Follow the instructions here if that is what you want. I've tried this but it was really slow and not very useful for me so I wouldn't recommend it

You can probably render your entire application and test it with Jest, but I wouldn't recommend that either. Jest tests should be designed to run fast and should only tests small units of your code. If you try and build tests that take a long time to run then there is an argument stating that your unit tests will become useless and developers will eventually not run them anymore.

If you are looking for tests that start an actual browser and click around like a user then have a look at Selenium which I would think is the most common approach these days

like image 100
Peter Avatar answered Oct 20 '22 14:10

Peter


This npm library can be integrated with your jest tests to run them in a browser :) : https://www.npmjs.com/package/jest-browser

I can't say how good it is/what the cons are but it looks like it is worth a try!

like image 34
Jack Wilkinson Avatar answered Oct 20 '22 14:10

Jack Wilkinson