Setup
If you're running your tests in the browser bundled with webpack (or similar) then DOM Testing Library should work out of the box for you. However, most people using DOM Testing Library are using it with the Jest testing framework with the testEnvironment set to jest-environment-jsdom
Using Without Jest
jsdom is a pure JavaScript implementation of the DOM and browser APIs that runs in node. If you're not using Jest and you would like to run your tests in Node, then you must install jsdom yourself. There's also a package called global-jsdom which can be used to setup the global environment to simulate the browser APIs. Note that if you're using Vitest you only need to configure environment to jsdom to achieve the same effect, you don't need global-jsdom.
First, install jsdom and global-jsdom.
- npm
- Yarn
npm install --save-dev jsdom global-jsdom
yarn add --dev jsdom global-jsdom
With mocha, the test command would look something like this:
mocha --require global-jsdom/register