Documentation

English
  • English
  • Russian
Welcome

info

Please note that zkApp programmability is not yet available on Mina Mainnet, but zkApps can now be deployed to Berkeley QANet.

How to test a zkApp

Learn how to write automated tests for your smart contract.

Writing automated tests for your smart contract is of crucial importance. The Jest testing framework is included in all projects created by the Mina zkApp CLI via zk project <name> and zk example <name. We recommend Jest, but any testing framework can be used.

Running tests

To run all test files within your project, run npm run test or npm run testw (for watch mode) from your project’s root directory.

To generate a test coverage report for your project, run npm run coverage. Coverage reports show the % of your code that is tested. The result will be output in your terminal. This can be helpful to ensure your code is well tested.

Writing tests

Creating tests for your smart contract is easy using the Mina zkApp CLI. To scaffold a TypeScript file with a corresponding test file, simply run the command zk file foo. This will generate two files, named foo.ts and foo.test.ts. foo.test.ts is a great place to start writing all your smart contract test code. To write good unit tests, it's vital that you concretely understand the functionality your smart contract provides. An example is shown below of a basic test written in Jest:

describe("foo.test.ts", () => {
  describe("test()", () => {
    // your test here
  });
});

Because we are using Jest, it's helpful to break down all functionality of your smart contract into describe blocks. Mapping out your unit tests like this is also a good way of providing documentation to other developers reading your smart contract. An excellent place to start testing your smart contract is in the areas your smart contract modifies its state. Make sure to verify that your state updates in the way you expect.

For examples of existing tests, we recommend creating a template example using the Mina zkApp CLI via zk project <name> and examining the test file there. You will see a basic example of a few tests that deploy and update the state on a smart contract using Jest.

Learn more

Please see the Jest docs for further information on how to use Jest.

We will be adding blockchain-specific testing functionality in the future. Keep an eye on this section for updates.

Next Steps

Now that you've learn how to test a smart contract, you can now learn how to deploy your zkApp.