Vitest Environment for Clarinet Projects

3.0.2 · active · verified Tue Apr 21

The `vitest-environment-clarinet` package provides a specialized Vitest testing environment tailored for developing and testing Clarity smart contracts within Clarinet projects. It enables developers to integrate the Clarinet simulation network (simnet) directly into their Vitest test suites, facilitating interactions with deployed smart contracts in a controlled environment. The current stable version is 3.0.2, with recent releases indicating a focus on maintaining compatibility with new Vitest major versions (e.g., supporting Vitest 4.x in v2.6.0 and v3.0.0). This package differentiates itself by offering a seamless, opinionated integration for Stacks blockchain development, leveraging Vitest's speed and features while providing the necessary Clarinet context. It is typically used alongside `@stacks/clarinet-sdk` to access the `clarity` testing utilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up `vitest-environment-clarinet` in `vitest.config.ts` and write basic TypeScript tests that interact with Clarinet smart contracts using the globally available `clarity` object provided by `@stacks/clarinet-sdk`.

/* package.json */
{
  "name": "my-clarinet-project",
  "version": "0.1.0",
  "description": "A Clarinet project with Vitest tests",
  "scripts": {
    "test": "vitest"
  },
  "devDependencies": {
    "@stacks/clarinet-sdk": ">=3.8.1",
    "vitest": "^1.0.0 || ^2.0.0 || ^3.0.0 || ^4.0.0",
    "vitest-environment-clarinet": "^3.0.0"
  }
}

/* vitest.config.ts */
/// <reference types="vitest/config" />
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    environment: 'clarinet',
    // Optional: Specify test file patterns
    include: ['**/*.test.ts'],
  },
});

/* tests/example.test.ts */
import { describe, test, expect } from 'vitest';
// clarity is globally available or via context from @stacks/clarinet-sdk
// when 'clarinet' environment is used.

describe('Clarinet Contract Interaction', () => {
  test('should deploy contracts and read a value', async () => {
    // Assuming 'hello-world' is a contract in your Clarinet project
    const contractAddress = 'ST0000000000000000000000000000000000000001.hello-world';
    
    // Example: Reading a public variable
    const response = await clarity.contract.callReadOnlyFn(
      contractAddress,
      'get-message',
      [], // No arguments
      clarity.deployerWallet.address
    );
    
    expect(response.result).toBe('"Hello, World!"');
  });

  test('should execute a public function', async () => {
    const contractAddress = 'ST0000000000000000000000000000000000000001.hello-world';
    
    const { result } = await clarity.submit.call(
      contractAddress,
      'set-message',
      [clarity.string('New Message')],
      clarity.deployerWallet
    );

    expect(result.isOk).toBe(true);
    // You can also assert on events, transactions, etc.
  });
});

view raw JSON →