npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stacks/clarunit

v0.0.2

Published

Clarunit, enables writing unit tests in Clarity

Downloads

68

Readme

clarunit

This package allows you to write unit tests for Clarity smart contracts in the Clarity language itself, as opposed to TypeScript. clarunit will automatically detect test files and test functions.

An example Clarinet-sdk project using clarunit can be found in the example folder.

Setup

  1. Install package @stacks/clarunit using your favourite package manager. (Be sure to pin the version!)
  2. Create a test file in your existing tests folder, you can use any name but using clarunit.test.ts is recommended.
  3. Add the following to the newly created file:
    import { clarunit } from "clarunit";
    clarunit(simnet);
  4. Run your tests per usual using npm test or yarn test.

clarunit takes configuration from Clarinet via Clarinet.toml. It automatically detects all instantiated test contracts.

Unit testing

Adding tests

To write unit tests, follow these steps:

  1. Create a new Clarity contract in the ./tests folder. It can have any name but it should end in _test.clar. Files that do not follow this convention are ignored. (For example: my-contract_test.clar will be included and my-contract.clar will not.)
  2. Add the new Clarity contract to Clarinet.toml.
  3. Write unit tests as public functions, the function name must start with test-.

Writing tests

Unit test functions should be public without parameters. If they return an ok response of any kind, the test is considered to have passed whereas an err indicates a failure. The failure value is printed so it can be used to provide a helpful message. The body of the unit test is written like one would usually write Clarity, using try! and unwrap! and so on as needed.

Example:

(define-public (test-my-feature)
	(begin
		(unwrap! (contract-call? .some-project-contract my-feature) (err "Calling my-feature failed"))
		(ok true)
	)
)

Prepare function

Sometimes you need to run some preparation logic that is common to all or multiple unit tests. If the script detects a function called prepare, it will be invoked before calling the unit test function itself. The prepare function should return an ok, otherwise the test fails.

(define-public (prepare)
	(begin
		(unwrap! (contract-call? .some-project-contract prepare-something) (err "Preparation failed"))
		(ok true)
	)
)

(define-public (test-something)
	;; prepare will be executed before running the test.
)

Annotations

You can add certain comment annotations before unit test functions to add information or modify behaviour. Annotations are optional.

| Annotation | Description | |-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------| | @name | Give the unit test a name, this text shows up when running unit tests. | | @no-prepare | Do not call the prepare function before running this unit test. | | @prepare | Override the default prepare function with another. The function name should follow the tag. | | @caller | Override the default caller when running this unit test. Either specify an account name or standard principal prefixed by a single tick '. | | @mine-blocks-before | Mine a number of blocks before running the test. The number of blocks should follow the tag. |

Examples:

(define-public (prepare) (ok "Default prepare function"))

(define-public (custom-prepare) (ok "A custom prepare function"))

;; A test without any annotations
(define-public (test-zero) (ok true))

;; @name A normal test with a name, the prepare function will run before.
(define-public (test-one) (ok true))

;; @name This test will be executed without running the default prepare function.
;; @no-prepare
(define-public (test-two) (ok true))

;; @name Override the default prepare function, it will run custom-prepare instead.
;; @prepare custom-prepare
(define-public (test-three) (ok true))

;; @name This test will be called with tx-sender set to wallet_1 (from the settings toml file).
;; @caller wallet_1
(define-public (test-four) (ok true))

;; @name This test will be called with tx-sender set to the specified principal.
;; @caller 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG
(define-public (test-five) (ok true))

;; @name Five blocks are mined before this test is executed.
;; @mine-blocks-before 5
(define-public (test-six) (ok true))