Skip to content

Commit 0712eaf

Browse files
eric-stacksgitbook-bot
authored andcommitted
GITBOOK-50: update Clarinet Vitest config
1 parent 95bd564 commit 0712eaf

File tree

1 file changed

+76
-13
lines changed

1 file changed

+76
-13
lines changed

docs/build/clarinet/project-structure.md

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

117117
### Vitest configuration
118118

119-
The **vitest.config.js** configures the testing framework:
120-
121-
```js
122-
123-
/// <reference types="vitest" />
124-
125-
import { defineConfig } from "vite";
126-
import { vitestSetupFilePath, getClarinetVitestsArgv } from "@stacks/clarinet-sdk/vitest";
119+
The **`vitest.config.ts`** (or `.js`) configures the testing framework. Make sure to import `defineConfig` from `vitest/config` (and not for `vite`). This configuration will work with Vitest v4 and higher.
120+
121+
{% code expandable="true" %}
122+
```typescript
123+
import { defineConfig } from "vitest/config";
124+
import {
125+
vitestSetupFilePath,
126+
getClarinetVitestsArgv,
127+
} from "@stacks/clarinet-sdk/vitest";
128+
129+
/*
130+
In this file, Vitest is configured so that it works seamlessly with Clarinet and the Simnet.
131+
The `vitest-environment-clarinet` will initialise the clarinet-sdk
132+
and make the `simnet` object available globally in the test files.
133+
`vitestSetupFilePath` points to a file in the `@stacks/clarinet-sdk` package that does two things:
134+
- run `before` hooks to initialize the simnet and `after` hooks to collect costs and coverage reports.
135+
- load custom vitest matchers to work with Clarity values (such as `expect(...).toBeUint()`)
136+
The `getClarinetVitestsArgv()` will parse options passed to the command `vitest run --`
137+
- vitest run -- --manifest ./Clarinet.toml # pass a custom path
138+
- vitest run -- --coverage --costs # collect coverage and cost reports
139+
*/
127140

128141
export default defineConfig({
129142
test: {
130-
environment: "clarinet", // use vitest-environment-clarinet
143+
// use vitest-environment-clarinet
144+
environment: "clarinet",
131145
pool: "forks",
132-
poolOptions: {
133-
threads: { singleThread: true },
134-
forks: { singleFork: true },
135-
},
146+
// clarinet handles test isolation by resetting the simnet between tests
147+
isolate: false,
148+
maxWorkers: 1,
136149
setupFiles: [
137150
vitestSetupFilePath,
138151
// custom setup files can be added here
@@ -146,6 +159,7 @@ export default defineConfig({
146159
},
147160
});
148161
```
162+
{% endcode %}
149163

150164
This configuration enables:
151165

@@ -154,10 +168,58 @@ This configuration enables:
154168
* **Coverage tracking**: Generate reports in multiple formats
155169
* **Custom setup**: Add project-specific test utilities
156170

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

159220
The **tsconfig.json** provides TypeScript support:
160221

222+
{% code expandable="true" %}
161223
```json
162224
{
163225
"compilerOptions": {
@@ -185,6 +247,7 @@ The **tsconfig.json** provides TypeScript support:
185247
]
186248
}
187249
```
250+
{% endcode %}
188251

189252
Properly setting the `include` property ensures TypeScript picks up the helpers defined in the Clarinet SDK package along with your tests.
190253

0 commit comments

Comments
 (0)