near-runner-ava v1.0.0
near-runner + AVA
A thin wrapper around near-runner to make it easier to use with AVA and TypeScript. If you don't want AVA, use near-runner directly.
Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment.
Quick Start
near-runner-init is a one-time command to quickly initialize a project with near-runner-ava. You will need NodeJS installed. Then:
npx near-runner-initIt will:
- Add a
near-runnerdirectory to the folder where you ran the command. This directory contains all the configuration needed to get you started with near-runner-ava, and a__tests__subfolder with a well-commented example test file. - Create
test.shandtest.batscripts in the folder where you ran the command. These can be used to quickly run the tests innear-runner. Feel free to integrate test-running into your project in a way that makes more sense for you, and then remove these scripts. Install NPM dependencies using
npm install. Most of the output you see when running the command comes from this step. You can skip this:npx near-runner-init --no-install.
Manual Install
Install.
npm install --save-dev near-runner-ava # npm yarn add --dev near-runner-ava # yarnConfigure.
AVA currently requires that your project have its own AVA config file. Add a file called
ava.config.cjsnext to yourpackage.jsonwith the following contents:module.exports = require('near-runner-ava/ava.config.cjs');We also recommend using the
near-runner-avascript to run your tests. This is mostly an alias forava, and passes CLI arguments right through."test": "near-runner-ava"Now you can run tests with
npm run testoryarn test.If you want to write tests with TypeScript (recommended), you can add a
tsconfig.jsonto your project root with the following contents:{"extends": "near-runner-ava/tsconfig.ava.json"}If you already have TypeScript set up and you don't want to extend the config from
near-runner-ava, feel free to just copy the settings you want from tsconfig.ava.json.Initialize.
Make a
__tests__folder, make your first test file. Call itmain.ava.tsif you're not sure what else to call it. The AVA config you extended above will find files that match the*.ava.(ts|js)suffix.In
main.ava.ts, set up arunnerwith NEAR accounts, contracts, and state that will be used in all of your tests.import {Runner} from 'near-runner-ava'; const runner = Runner.create(async ({root}) => { const alice = await root.createAccount('alice'); const contract = await root.createAndDeploy( 'contract-account-name', 'path/to/compiled.wasm' ); // make other contract calls that you want as a starting point for all tests return {alice, contract}; });Write tests.
runner.test("does something", async (test, { alice, contract }) => { await alice.call(contract, "some_update_function", { some_string_argument: "cool", some_number_argument: 42, }); const result = await contract.view("some_view_function", { account_id: alice, }); // When --verbose option is used this will print neatly underneath the test in the output. test.log(result) test.is(result, "whatever"); }); runner.test("does something else", async (test, { alice, contract }) => { const result = await contract.view("some_view_function", { account_id: alice, }); test.is(result, "some default"); });runner.testis added tonear-runnerbynear-runner-ava, and is shorthand for:import avaTest from 'ava'; avaTest('does something', async test => { await runner.run(async ({…}) => { // tests go here }); });Where
testandtcome from AVA andrunner.runcomes from near-runner.
See the __tests__ directory for more examples.
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago