Write Unit Tests for a Wix CLI Project

The Wix CLI is in Developer Preview and is subject to change.

CLI Documentation Notice

You're viewing documentation for the new Wix CLI, which we recommend for all new projects.

This article explains how to write unit tests for a CLI project using Vitest and @testing-library/react.

Note: While this article's flow uses Vitest specifically, the CLI is framework-agnostic and supports any testing library.

This article covers:

  • Preparing your project for unit testing with Vitest.
  • Writing unit tests for your project.

Before you begin

Step 1 | Configure your project for tests

  1. Run the following command to install the required packages:

    Copy

    Note: Since the Wix CLI uses React 16 for dashboard extensions, we currently only support version 12 of @testing-library/react.

  2. Create or update your vitest.config.js file with the following configuration:

    Copy
  3. Create a test setup file at tests/setup.ts. This file is used for general test configuration with Vitest:

    Copy
  4. Add the following script to your package.json file to trigger unit tests:

    Copy

Step 2 | Write unit tests

Depending on your project's functionality, testing your project's react components may involve mocking Wix JavaScript SDK APIs, backend interactions, or both.

Test a react component without the SDK or backend interactions

To test a react component like a dashboard page, check that aspects of the component would be rendered correctly.

For example, given the following dashboard page:

Copy

You can test whether the "Dashboard Page" text would be present on the page when rendered.

Copy

Test a component that involves an SDK API call

If your component's code calls a Wix JavaScript SDK API, render the component and manually mock the API.

The following example demonstrates how to mock the Dashboard API's showToast() method.

Copy

Test a component that involves backend interactions

If your project interacts with a backend, mock those interactions using Vitest.

For example, if you have a page that calls the CRM backend to retrieve user contacts:

Copy

You would mock the backend interactions with the API using vitest as follows:

Copy

See also

Did this help?