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:
Install scryptlib
npm i scryptlib
Hint
scryptlib is the official Javascript/TypeScript SDK for integrating Bitcoin smart contracts written in sCrypt.
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.
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.
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'));
Create an instance of the contract
const instance = new MyContract(1234, true, ...parameters);
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;
Start the debugger
IDE allows the debugger to be started where the unit test fails, which will help developers greatly improve the efficiency of debugging contract issues. If the unit test fails, there are two additional lines of information in the error message:
Go to Source : It shows the specific line in the contract code where the unit test failed, and the script opcode that was executed last. CRTL + click will jump directly to the line where the error occurred;
Launch Debugger: CRTL + Click the link will directly start the debugger session based on the arguments in the test. The debugger will pause just before the error occurs in order to check the cause of the failure. Using this function can greatly improve the efficiency of debugging.
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