Skip to content

Commit 6f2e990

Browse files
feat: create clarinet-sdk testing boilerplate (#1158)
* feat: create clarinet-sdk testing boilerplate * refactor: remvoe useless shebang * fix: typo in components/clarinet-sdk/src-ts/bin/index.ts Co-authored-by: janniks <[email protected]> --------- Co-authored-by: janniks <[email protected]>
1 parent 4a030f4 commit 6f2e990

11 files changed

+621
-661
lines changed

components/clarinet-sdk/README.md

+13-112
Original file line numberDiff line numberDiff line change
@@ -45,134 +45,35 @@ It's also possible to provide the path to the manifest like so:
4545

4646
## Tests
4747

48-
> Note: A bit of boilerplate is needed to setup the testing environment. Soon it will be handled by the clarinet-cli.
49-
50-
The SDK can be used to write unit-tests for Clarinet projects.
51-
Make sure you are in directory with a Clarinet.toml file and the associated Clarity smart contracts:
52-
53-
```sh
54-
cd ./my-project
55-
ls # here you should see the Clarinet.toml file
56-
```
48+
The SDK can be used to write unit tests for Clarinet projects.
5749

58-
Let's initialize the Node.js project:
59-
```sh
60-
npm init -y # the -y option sets default properties
61-
npm install @hirosystems/clarinet-sdk @stacks/transactions vite vitest vitest-environment-clarinet
62-
```
63-
64-
Update the package.json file scripts to handle tests:
65-
```json
66-
"scripts": {
67-
"test": "vitest run",
68-
"test:coverage": "vitest run -- --coverage true"
69-
},
70-
```
50+
You'll need to have Node.js (>= 18) and NPM setup. If you are not sure how to set it up, [Volta](https://volta.sh/) is a nice tool to get started.
7151

72-
The `./.gitignore` file also needs to be updated, add the following lines at the end. It is especially important to ignore `node_modules`.
73-
```
74-
logs
75-
*.log
76-
npm-debug.log*
77-
coverage
78-
*.info
79-
node_modules
80-
```
52+
In the terminal, run `node --version` to make sure it's available and up to date.
8153

82-
A config file is needed for Vitest to use the clarinet-environment.
83-
Create the file `vitest.config.js` with the following content:
84-
```js
85-
/// <reference types="vitest" />
86-
87-
import { defineConfig } from "vite";
88-
import { vitestSetupFilePath, getClarinetVitestsArgv } from "@hirosystems/clarinet-sdk/vitest";
89-
90-
export default defineConfig({
91-
test: {
92-
environment: "clarinet",
93-
singleThread: true,
94-
setupFiles: [vitestSetupFilePath],
95-
environmentOptions: {
96-
clarinet: getClarinetVitestsArgv(),
97-
},
98-
},
99-
});
100-
```
54+
> Note: A bit of boilerplate is needed to setup the testing environment. Soon it will be handled by the clarinet-cli.
10155
102-
The set up is ready, let's write the first test. Create a test file in the `unit-tests` directory:
56+
Open your terminal and go to a new or existing Clarinet project:
10357

10458
```sh
105-
mkdir unit-tests
106-
touch unit-tests/my-contract.test.js
107-
```
108-
109-
```js
110-
// unit-tests/my-contract.test.js
111-
import { describe, it, expect } from "vitest";
112-
import { Cl } from "@stacks/transactions";
113-
114-
describe("test counter ONE", () => {
115-
const accounts = vm.getAccounts();
116-
const w1 = accounts.get("wallet_1");
117-
if (!w1) throw new Error("wallet_1 does not exist");
118-
119-
it("adds two numbers", () => {
120-
const callAdd = vm.callPublicFn("my-contract", "add", [Cl.uint(21), Cl.uint(21)], w1);
121-
expect(callAdd.result).toBeOk(Cl.uint(42));
122-
});
123-
});
124-
59+
cd my-project
60+
ls # you should see a Clarinet.toml file in the list
12561
```
12662

127-
### Notes:
63+
Run the following command to setup the testing framework:
12864

129-
- This code assumes that you have a contract called `my-contract` with a method `add`.
130-
```clar
131-
;; contracts/my-contract.clar
132-
(define-public (add (n1 uint) (n2 uint))
133-
(ok (+ n1 n2))
134-
)
65+
```sh
66+
npx @hirosystems/clarinet-sdk
13567
```
13668

137-
- You may need to disable the deno extension if it's activated in `.vscode/settings.json`.
69+
Visit the [clarity starter project](https://github.com/hirosystems/clarity-starter/tree/170224c9dd3bde185f194a9036c5970f44c596cd) to see the testing framework in action.
13870

13971

14072
### Type checking
14173

142-
You can use TypeScript to write test by installing it and setting up the `tsconfig.json`.
143-
144-
```sh
145-
npm install typescript
146-
touch tsconfig.json
147-
```
148-
149-
```json
150-
{
151-
"compilerOptions": {
152-
"target": "ESNext",
153-
"useDefineForClassFields": true,
154-
"module": "ESNext",
155-
"lib": ["ESNext"],
156-
"skipLibCheck": true,
157-
158-
"moduleResolution": "bundler",
159-
"allowImportingTsExtensions": true,
160-
"resolveJsonModule": true,
161-
"isolatedModules": true,
162-
"noEmit": true,
163-
164-
"strict": true,
165-
"noImplicitAny": true,
166-
"noUnusedLocals": true,
167-
"noUnusedParameters": true,
168-
"noFallthroughCasesInSwitch": true
169-
},
170-
"include": ["node_modules/@hirosystems/clarinet-sdk/vitest-helpers/src", "unit-tests"]
171-
}
172-
173-
```
74+
We recommend to use TypeScript to write the unit tests, but it's also possible to do it with JavaScript. To do so, rename your test files to `.test.js` instead of `.test.ts`. You can also delete the `tsconfig.json` and uninstall typescript with `npm uninstall typescript`.
17475

175-
If you want to write your test in JavaScript but still have a certain level of type safety and autocompletion, VSCode can help you with that. You can create a basic `jsconfig.json` file:
76+
Note: If you want to write your test in JavaScript but still have a certain level of type safety and autocompletion, VSCode can help you with that. You can create a basic `jsconfig.json` file:
17677

17778
```json
17879
{

0 commit comments

Comments
 (0)