close
  • English
  • testEnvironment

    • Type: 'node' | 'jsdom' | 'happy-dom' | { name: EnvironmentName, options?: EnvironmentOptions }
    • Default: 'node'
    • CLI: --testEnvironment=node

    The environment that will be used for testing.

    The default environment in Rstest is a Node.js environment. If you are building a web application, you can use a browser-like environment through jsdom or happy-dom instead.

    CLI
    rstest.config.ts
    npx rstest --testEnvironment=jsdom

    DOM testing

    Rstest supports jsdom and happy-dom for mocking DOM and browser APIs.

    If you want to enable DOM testing, you can use the following configuration:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      testEnvironment: 'jsdom', // or 'happy-dom'
    });

    You also need to install the corresponding package:

    For jsdom

    npm
    yarn
    pnpm
    bun
    deno
    npm add jsdom -D

    For happy-dom

    npm
    yarn
    pnpm
    bun
    deno
    npm add happy-dom -D

    After enabling DOM testing, you can write tests that use browser APIs like document and window.

    test('DOM test', () => {
      document.body.innerHTML = '<p class="content">hello world</p>';
      const paragraph = document.querySelector('.content');
      expect(paragraph?.innerHTML).toBe('hello world');
    });

    Environment options

    You can also pass options to the test environment. This is useful for configuring jsdom or happy-dom. For example, you can set the url for jsdom:

    rstest.config.ts
    import { defineConfig } from '@rstest/core';
    
    export default defineConfig({
      testEnvironment: {
        name: 'jsdom',
        options: {
          // jsdom-specific options
          url: 'https://example.com',
        },
      },
    });

    The options object is passed directly to the environment's constructor.

    • For jsdom, it's passed to the JSDOM constructor. You can find available options in the jsdom documentation.
    • For happy-dom, it's passed to the Window constructor. You can find available options in the happy-dom documentation.

    Environment comments

    You can override the environment for a single test file by adding an environment comment near the top of the file:

    example.test.ts
    // @rstest-environment jsdom
    
    test('DOM test', () => {
      document.body.innerHTML = '<p>hello world</p>';
      expect(document.querySelector('p')?.textContent).toBe('hello world');
    });

    Use @rstest-environment-options to pass environment options for the annotated file. The options must be a single-line JSON object:

    example.test.ts
    // @rstest-environment jsdom
    // @rstest-environment-options { "url": "https://example.com/" }
    
    test('sets the jsdom url', () => {
      expect(window.location.href).toBe('https://example.com/');
    });

    Rstest also recognizes @vitest-environment and @jest-environment aliases, including their -options variants, to make migration easier.

    Environment comments support the built-in Node runner environments: node, jsdom, and happy-dom. They do not apply to browser mode. If most files use the same environment, prefer configuring testEnvironment or separate projects in rstest.config.ts.

    Examples