Skip to content

Commit 4874405

Browse files
committed
fix: clarinet vitest config
1 parent d38d1f1 commit 4874405

File tree

1 file changed

+79
-10
lines changed

1 file changed

+79
-10
lines changed

docs/reference/clarinet/project-structure.md

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,41 @@ The **package.json** defines your testing environment and dependencies:
116116

117117
### Vitest configuration
118118

119-
The **vitest.config.js** configures the testing framework:
119+
The **`vitest.config.ts`** (or `.js`) configures the testing framework.
120+
Make sure to import `defineConfig` from `vitest/config` (and not for `vite`).
120121

121-
```js
122+
This configuration will work with Vitest v4 and higher
122123

123-
/// <reference types="vitest" />
124+
```ts
125+
import { defineConfig } from "vitest/config";
126+
import {
127+
vitestSetupFilePath,
128+
getClarinetVitestsArgv,
129+
} from "@stacks/clarinet-sdk/vitest";
124130

125-
import { defineConfig } from "vite";
126-
import { vitestSetupFilePath, getClarinetVitestsArgv } from "@stacks/clarinet-sdk/vitest";
131+
/*
132+
In this file, Vitest is configured so that it works seamlessly with Clarinet and the Simnet.
133+
134+
The `vitest-environment-clarinet` will initialise the clarinet-sdk
135+
and make the `simnet` object available globally in the test files.
136+
137+
`vitestSetupFilePath` points to a file in the `@stacks/clarinet-sdk` package that does two things:
138+
- run `before` hooks to initialize the simnet and `after` hooks to collect costs and coverage reports.
139+
- load custom vitest matchers to work with Clarity values (such as `expect(...).toBeUint()`)
140+
141+
The `getClarinetVitestsArgv()` will parse options passed to the command `vitest run --`
142+
- vitest run -- --manifest ./Clarinet.toml # pass a custom path
143+
- vitest run -- --coverage --costs # collect coverage and cost reports
144+
*/
127145

128146
export default defineConfig({
129147
test: {
130-
environment: "clarinet", // use vitest-environment-clarinet
148+
// use vitest-environment-clarinet
149+
environment: "clarinet",
131150
pool: "forks",
132-
poolOptions: {
133-
threads: { singleThread: true },
134-
forks: { singleFork: true },
135-
},
151+
// clarinet handles test isolation by resetting the simnet between tests
152+
isolate: false,
153+
maxWorkers: 1,
136154
setupFiles: [
137155
vitestSetupFilePath,
138156
// custom setup files can be added here
@@ -145,6 +163,7 @@ export default defineConfig({
145163
},
146164
},
147165
});
166+
148167
```
149168

150169
This configuration enables:
@@ -154,6 +173,56 @@ This configuration enables:
154173
* **Coverage tracking**: Generate reports in multiple formats
155174
* **Custom setup**: Add project-specific test utilities
156175

176+
<details>
177+
178+
<summary>If you use Vitest 3 or lower and don't want to upgrade now, you can use this config</summary>
179+
180+
```ts
181+
import { defineConfig } from "vitest/config";
182+
import {
183+
vitestSetupFilePath,
184+
getClarinetVitestsArgv,
185+
} from "@stacks/clarinet-sdk/vitest";
186+
187+
/*
188+
In this file, Vitest is configured so that it works seamlessly with Clarinet and the Simnet.
189+
190+
The `vitest-environment-clarinet` will initialise the clarinet-sdk
191+
and make the `simnet` object available globally in the test files.
192+
193+
`vitestSetupFilePath` points to a file in the `@hirosystems/clarinet-sdk` package that does two things:
194+
- run `before` hooks to initialize the simnet and `after` hooks to collect costs and coverage reports.
195+
- load custom vitest matchers to work with Clarity values (such as `expect(...).toBeUint()`)
196+
197+
The `getClarinetVitestsArgv()` will parse options passed to the command `vitest run --`
198+
- vitest run -- --manifest ./Clarinet.toml # pass a custom path
199+
- vitest run -- --coverage --costs # collect coverage and cost reports
200+
*/
201+
202+
export default defineConfig({
203+
test: {
204+
// use vitest-environment-clarinet
205+
environment: "clarinet",
206+
pool: "forks",
207+
poolOptions: {
208+
forks: { singleFork: true },
209+
},
210+
setupFiles: [
211+
vitestSetupFilePath,
212+
// custom setup files can be added here
213+
],
214+
environmentOptions: {
215+
clarinet: {
216+
...getClarinetVitestsArgv(),
217+
// add or override options
218+
},
219+
},
220+
},
221+
});
222+
223+
```
224+
</details>
225+
157226
### TypeScript configuration
158227

159228
The **tsconfig.json** provides TypeScript support:

0 commit comments

Comments
 (0)