close
  • English
  • Quick start

    Online examples

    You can try Rstest online without any setup by using the following examples:

    Setup environment

    Before getting started, you will need to install Node.js >= 20.19.0, it is recommended to use the Node.js LTS version.

    Check the current Node.js version with the following command:

    node -v

    If you do not have Node.js installed in current environment, or the installed version is too low, you can use nvm or fnm to install.

    Here is an example of how to install via nvm:

    # Install Node.js LTS
    nvm install --lts
    # Switch to Node.js LTS
    nvm use --lts

    Using Rstest

    Agent prompt

    If you are using a coding agent, you can copy the following prompt to set up Rstest automatically:

    For your Agent
    Set up Rstest

    Copy this prompt and send it to your coding agent.

    If you are migrating from an existing Jest or Vitest project, use this prompt:

    For your Agent
    Migrate to Rstest

    Copy this prompt and send it to your coding agent.

    Manual installation

    You can install Rstest using the following command:

    npm
    yarn
    pnpm
    bun
    deno
    npm add @rstest/core -D

    Next, you need to update the npm scripts in your package.json to use Rstest's CLI commands.

    package.json
    {
      "scripts": {
        "test": "rstest"
      }
    }

    After completing the above steps, you can run the Rstest tests using npm run test, yarn test, or pnpm test. Alternatively, you can directly use npx rstest to execute the Rstest tests.

    Rstest has built-in commands such as watch and run, please refer to CLI Tools to learn about all available commands and options.

    Writing tests

    As a simple example, we have a sayHi method. To test it, you can create a test file called index.test.ts or use In-Source test similar to Rust Test.

    index.ts
    export const sayHi = () => 'hi';
    index.test.ts
    import { expect, test } from '@rstest/core';
    import { sayHi } from '../src/index';
    
    test('should sayHi correctly', () => {
      expect(sayHi()).toBe('hi');
    });

    Next, you can execute the test by using the command configured in Using Rstest. Rstest will print the following message:

     test/index.test.ts (1)
    
     Test Files 1 passed
          Tests 1 passed
       Duration 140 ms (build 17 ms, tests 123 ms)

    Next steps

    If you want to see complete project structures, refer to these common examples:

    • node: Node.js testing and coverage collection.
    • react: React component, hook, and SSR tests with happy-dom.
    • vue: Vue component and SSR testing.
    • browser-react: React component testing in Browser Mode.

    For more examples, see Rstest examples.