Testing

We test the contract by writing Javascript/Typescript. There are many contract test cases in Boilerplate.

First, run npm init in the project root directory to create an npm project. Next install dependencies:

  1. Install scryptlib

npm i scryptlib

Hint

scryptlib is the official Javascript/TypeScript SDK for integrating Bitcoin smart contracts written in sCrypt.

  1. Install mocha test framework

npm i -D chai mocha

Hint

We recommend using the mocha testing framework for contract testing. Of course you can also use other testing frameworks.

  1. Configure test commands in package.json

There should be a script named single-test in the script section of the package.json file. IDE uses it to run single file tests. Usually it looks like "single-test": "mocha", but you can customize it.

{
    "name": "helloworld",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "mocha -r ts-node/register tests/**/*scrypttest.*  --reporter spec --timeout 600000",
        "single-test": "mocha --reporter spec --timeout 120000" //测试命令
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "scryptlib": "^0.3.10"
    },
    "devDependencies": {
        "chai": "^4.3.4",
        "mocha": "^8.4.0"
    }
}

IDE supports running unit test files in the context menu of code editor/resource manager. After starting to run, IDE will automatically open the bottom panel of VS Code for you, and display the Output view. The test report will be shown in the Output panel.

Note

The test file must be suffixed with .scrypttest.js or .scrypttest.ts, otherwise the “Run sCrypt Test” option will not appear in the menu.

_images/run_testting.gif
  1. When testing a contract, you can use the contract description file (also just the _desc.json file) that loads the compiled output, such as:

const MyContract = buildContractClass(JSON.parse(descFileContent));

You can also use scryptlib to export the compileContract or compile function to compile the contract.

const MyContract = buildContractClass(compileContract('demo.scrypt'));
  1. Create an instance of the contract

const instance = new MyContract(1234, true, ...parameters);
  1. Execute the public function of the contract to verify the correctness of the contract.

const funcCall = instance.someFunc(new Sig('0123456'), new Bytes('aa11ff'), ...parameters);
const result = funcCall.verify(context);
expect(result.success, result.error).to.be.true;

Launch Debugger Command

Normally, you can use the right-click menu to run the unit test, so you can click the link in the test report from Output to open the debugger. But in some cases, the test runs in an external environment, such as a terminal. In this case, you will not be able to open the debugger by clicking the link in the test report. In this case, you can use the Launch Debugger command: paste scryptlaunch:///{file} into the command input pop-up box, and press Enter to start the debugger.

Note

Even if the contract to be debugged is not in the current project, you can start the debugger

_images/scryptlaunch.gif