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

wasm-mock-vm

v1.2.1

Published

Mock Near VM core logic for unit testing

Downloads

10

Readme

nearcore's VMlogic compiled to WebAssembly

Near smart contracts have access to a runtime API to interact with the host. Nearcore is written in Rust, which means it can be compiled into WebAsssembly. By compiling the core VM logic, a mock version of the runtime can be used to test smart contracts without the overhead of running a local node or using the testnet.

Currently rust smart contracts are unit tested by compiling the contract alongside the runtime since they are both in the same language. This solution shares the same memory at runtime, where as in the actual VM there is a separation between the host memory and the contract's memory.

This project uses wasm-bindgen, to create javascript bindings for the generated WebAssembly, which then can used along side the as-pect to write unit tests in AssemblyScript.

Setup

Node Version

Since the runtime uses u64, it requires the use of the node flag --experimental-wasm-bigint, which is included by default in imports.js of this project, however, this still requires a version of node >= v12.0.0.

Installation

In a blank Near AssemblyScript project you need to install the following dependencies:

npm install --save-dev wasm-mock-vm @as-pect/cli

Next set up as-pect with its init command.

npx asc --init

This creates a __tests__ folder and a as-pect.config.js file in the root of your project. Replace the contents of the as-pect.config.js with

module.exports = require("wasm-mock-vm/imports");

This will configure mock Near VM and as-pect.

Setting up Types

To ensure that the types added by the runtime library are detected by your IDE include a type declaration file in your root assembly folder, e.g. assembly/types.d.ts with the following line:

/// <reference types="near-runtime-ts/assembly/as_types" />

Example Usage

In assembly/contract.ts:

//@nearfile

export class Contract {
  constructor(public name: string) {}
}

and assembly/__tests__/contract.spec.ts

import { storage } from "near-runtime-ts";
import { Contract } from "../contract";

describe("Contract", () => {
  it("should be able to be stored and retrieved", () => {
    const contract = new Contract("unique name!");
    storage.set("contract", contract);
    const otherContract = storage.get<Contract>("contract");
    expect(otherContract.name).toBe(contract.name, "contracts should have the same name");
    expect(otherContract.name).not.toBe("", "retrieved contract's name should not be the empty string");
  });
});

API

By default the context used by the VM is a local context.json file. It first looks in the project's assembly/__test__ folder, then the project's root folder, and finally will read the default file in this project's root.

Each runtime API call by default updates the state of the VM. So in the example above any tests run after it will have "contract" in the storage. To prevent this there is an API to save and restore the state. as-pect has special functions to run before and after tests, e.g. beforeAll/beforeEach.

import { storage } from "near-runtime-ts";
import { Contract } from "../contract";
import { VM } from "wasm-mock-vm";

describe("Contract", () => {

  beforeEach(() => {
    VM.saveState();
  });

  afterEach(() => {
    VM.restoreState();
  });
  
  it("should be able to be stored and retrieved", () => {
    const contract = new Contract("unique name!");
    storage.set("contract", contract);
    const otherContract = storage.get<Contract>("contract");
    expect(contract.name).toBe(otherContract.name, "contracts should have the same name");
    expect(contract.name).toBe("", "contracts should have the same name");
  });

  it("state should not contain updates of previous test", () => {
    expect(storage.contain("contract")).toBe(false, "contract shouldn't exist is original storage");
  });
});

The state also includes the registers that the runtime uses to store values in the host memory.