Happy DOM Documentation

repository·master·Indexed 26 days ago

https://github.com/capricorn86/happy-dom

A lightweight, headless JavaScript implementation of a web browser designed for high performance in testing environments and server-side rendering. It provides essential DOM APIs, including Custom Elements, Declarative Shadow DOM, Mutation Observer, Tree Walker, and the Fetch API. The project includes specialized packages such as @happy-dom/global-registrator for environment setup, @happy-dom/jest-environment for Jest integration, @happy-dom/node-canvas-adapter for canvas support via node-canvas, and @happy-dom/server-renderer for SSR and SSG.

Tokens
4.8K
Snippets
16
Records
39
Agent score
88%

What's inside happy-dom

  1. Overview of Happy DOM features

    master

    Happy DOM is a JavaScript implementation of a web browser without a graphical user interface. It is designed for high performance in environments like testing and server-side rendering. Key supported DOM features include:

    • Custom Elements (Web Components)
    • Declarative Shadow DOM
    • Mutation Observer
    • Tree Walker
    • Fetch API
  2. Pass Happy DOM Browser Settings to Jest

    master

    You can pass Happy DOM Browser Settings to the Jest environment using the testEnvironmentOptions key in your Jest configuration. This allows you to customize properties like the url, viewport dimensions (width, height), and settings (such as navigator.userAgent).

    {
      "testEnvironment": "@happy-dom/jest-environment",
      "testEnvironmentOptions": {
        "url": "http://localhost",
        "width": 1920,
        "height": 1080,
        "settings": {
          "navigator": {
             "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
          }
        }
      }
    }
  3. Use HappyDOMEnvironment as a Jest test environment

    master

    To use Happy DOM in your Jest tests, set happy-dom/jest-environment as your testEnvironment in your Jest configuration. This environment provides a Window instance and sets up global browser APIs within the Jest execution context.

    Configuration Options

    You can pass options to the environment via testEnvironmentOptions in your Jest config. Supported options include:

    • settings: An object of type IOptionalBrowserSettings to configure the Happy DOM instance.
    • customExportConditions: An array of strings to specify custom export conditions.
    • console: A custom console implementation.
  4. Use @happy-dom/node-canvas-adapter with Happy DOM

    master

    To enable canvas support in a Happy DOM Window instance, import CanvasAdapter from @happy-dom/node-canvas-adapter and pass it to the canvasAdapter option within the settings object of the Window constructor. You can also optionally set enableImageFileLoading: true to support loading image files (e.g., for <img> elements).

    import { Window } from 'happy-dom';
    import { CanvasAdapter } from '@happy-dom/node-canvas-adapter';
    
    const window = new Window({
      settings: {
        canvasAdapter: new CanvasAdapter(),
        // Optionally, enable image file loading (e.g. for <img> elements)
        enableImageFileLoading: true
      }
    });
    
    const canvas = window.document.createElement('canvas');
    const context = canvas.getContext('2d');
    
    canvas.width = 200;
    canvas.height = 200;
    
    // Now you can use canvas context
    context.fillStyle = 'red';
    context.fillRect(0, 0, 100, 100);
    
    // Get data URL
    const dataUrl = canvas.toDataURL();
    
    // Output the data URL
    console.log(dataUrl);