Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarinet unit tests revamp POC #1052

Closed
hugocaillard opened this issue Jun 15, 2023 · 1 comment
Closed

Clarinet unit tests revamp POC #1052

hugocaillard opened this issue Jun 15, 2023 · 1 comment
Assignees
Milestone

Comments

@hugocaillard
Copy link
Collaborator

hugocaillard commented Jun 15, 2023

Context

Some initial context in this discussion

TL;DR

  • Clarinet currently embeds Deno to run clarinet tests
  • This dependency is heavy, painful to maintain, and comes with build issues
  • We decided to explore another approach for Clarinet test

The POC

Clarinet will expose a JS library (through wasm) that will allow to test smart contracts in any JS/WASM environment (Node, Deno, Web Browers, etc).

import { main } from "../../../../hiro/clarinet/components/clarinet-sdk/dist/index.js";
import { before, describe, it } from "node:test";
import assert from "node:assert/strict";
import { Cl } from "@stacks/transactions";

let sender = "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5";

describe("test counter", () => {
  let session;

  before(async () => {
    session = await main();
    await session.initSession(process.cwd(), "./Clarinet.toml");
  });

  it("gets counter value", () => {
    const res = session.callReadOnlyFn("counter", "get-counter", [], sender);
    assert.deepEqual(Cl.int(0), res.result);
    assert.equal(session.blockHeight, 1);
  });

  it("increments counter value", () => {
    const incrRes = session.callPublicFn("counter", "increment", [], sender);
    assert.deepEqual(incrRes.result, Cl.ok(Cl.bool(true)));
    assert.equal(session.blockHeight, 2);

    let res = session.callReadOnlyFn("counter", "get-counter", [], sender);
    assert.deepEqual(res.result, Cl.int(1));
    assert.equal(session.blockHeight, 2);
  });

  it("add any value", () => {
    const addRes = session.callPublicFn("counter", "add", [Cl.int(10)], sender);
    assert.deepEqual(addRes.result, Cl.ok(Cl.bool(true)));
    assert.equal(session.blockHeight, 3);

    let res = session.callReadOnlyFn("counter", "get-counter", [], sender);
    assert.deepEqual(res.result, Cl.int(11));
    assert.equal(session.blockHeight, 3);
  });
});

Going further

A lot of features and improvements have to be considered in the next iteration (Q3). This is just a list of ideas, not all of them will be implemented

@hugocaillard
Copy link
Collaborator Author

This POC is now considered done.
Conclusion:

  • clarity sessions can easily be initiated in JS through wasm
  • developer can easily interact with the session (call contracts, interact with raw clarity values and (de)serialize it with stacks.js)

Let's pursue in this direction to build a enjoyable testing experience!

Closing in favor of #1065

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

1 participant