@algorandfoundation/algorand-typescript-testing 1.0.0-beta.16 → 1.0.0-beta.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +205 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # Algorand TypeScript Testing
2
+
3
+ [![docs-repository](https://img.shields.io/badge/url-repository-74dfdc?logo=github&style=flat.svg)](https://github.com/algorandfoundation/algorand-typescript-testing/)
4
+ [![learn-AlgoKit](https://img.shields.io/badge/learn-AlgoKit-74dfdc?logo=algorand&mac=flat.svg)](https://developer.algorand.org/algokit/)
5
+ [![github-stars](https://img.shields.io/github/stars/algorandfoundation/algorand-typescript-testing?color=74dfdc&logo=star&style=flat)](https://github.com/algorandfoundation/algorand-typescript-testing)
6
+ [![visitor-badge](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fgithub.com%2Falgorandfoundation%2Falgorand-typescript-testing&countColor=%2374dfdc&style=flat)](https://github.com/algorandfoundation/algorand-typescript-testing/)
7
+
8
+ `algorand-typescript-testing` is a companion package to [Algorand Typescript](https://github.com/algorandfoundation/puya-ts/tree/main/packages/algo-ts) that enables efficient unit testing of Algorand TypeScript smart contracts in an offline environment. This package emulates key AVM behaviors without requiring a network connection, offering fast and reliable testing capabilities with a familiar TypeScript interface.
9
+
10
+ The `algorand-typescript-testing` package provides:
11
+
12
+ - A simple interface for fast and reliable unit testing
13
+ - An offline testing environment that simulates core AVM functionality
14
+ - A familiar TypeScript experience, compatible with testing frameworks like [vitest](https://vitest.dev/), and [jest](https://jestjs.io/)
15
+
16
+ ## Quick Start
17
+
18
+ `algorand-typescript` is a prerequisite for `algorand-typescript-testing`, providing stubs and type annotations for Algorand TypeScript syntax. It enhances code completion and type checking when writing smart contracts. Note that this code isn't directly executable in standard Node.js environment; it's compiled by `puya-ts` into TEAL for Algorand Network deployment.
19
+
20
+ Traditionally, testing Algorand smart contracts involved deployment on sandboxed networks and interacting with live instances. While robust, this approach can be inefficient and lacks versatility for testing Algorand TypeScript code.
21
+
22
+ Enter `algorand-typescript-testing`: it leverages TypeScript's rich testing ecosystem for unit testing without network deployment. This enables rapid iteration and granular logic testing.
23
+
24
+ > **NOTE**: While `algorand-typescript-testing` offers valuable unit testing capabilities, it's not a replacement for comprehensive testing. Use it alongside other test types, particularly those running against the actual Algorand Network, for thorough contract validation.
25
+
26
+ ### Prerequisites
27
+
28
+ - Python 3.12 or later
29
+ - [Algorand Python](https://github.com/algorandfoundation/puya)
30
+ - Node.js 20.x or later
31
+ - [Algorand TypeScript](https://github.com/algorandfoundation/puya-ts)
32
+
33
+ ### Installation
34
+
35
+ `algorand-typescript-testing` is distributed via [npm](https://www.npmjs.com/package/@algorandfoundation/algorand-typescript-testing/). Install the package using `npm`:
36
+
37
+ ```bash
38
+ npm i @algorandfoundation/algorand-typescript-testing
39
+ ```
40
+
41
+ ### Testing your first contract
42
+
43
+ Let's write a simple contract and test it using the `algorand-typescript-testing` framework.
44
+
45
+ If you are using [vitest](https://vitest.dev/) with [@rollup/plugin-typescript](https://www.npmjs.com/package/@rollup/plugin-typescript) plugin, configure `puyaTsTransformer` as a `before` stage transformer of `typescript` plugin in `vitest.config.mts` file.
46
+
47
+ ```typescript
48
+ import typescript from '@rollup/plugin-typescript'
49
+ import { defineConfig } from 'vitest/config'
50
+ import { puyaTsTransformer } from '@algorandfoundation/algorand-typescript-testing/test-transformer'
51
+
52
+ export default defineConfig({
53
+ esbuild: {},
54
+ test: {
55
+ setupFiles: 'vitest.setup.ts',
56
+ },
57
+ plugins: [
58
+ typescript({
59
+ tsconfig: './tsconfig.json',
60
+ transformers: {
61
+ before: [puyaTsTransformer],
62
+ },
63
+ }),
64
+ ],
65
+ })
66
+ ```
67
+
68
+ `algorand-typescript-testing` package also exposes additional equality testers which enables the smart contract developers to write terser test by avoiding type casting in assertions. It can setup in `beforeAll` hook point in the setup file, `vitest.setup.ts`.
69
+
70
+ ```typescript
71
+ import { beforeAll, expect } from 'vitest'
72
+ import { addEqualityTesters } from '@algorandfoundation/algorand-typescript-testing'
73
+
74
+ beforeAll(() => {
75
+ addEqualityTesters({ expect })
76
+ })
77
+ ```
78
+
79
+ #### Contract Definition
80
+
81
+ ```typescript
82
+ import { arc4, assert, Bytes, GlobalState, gtxn, LocalState, op, Txn, uint64, Uint64 } from '@algorandfoundation/algorand-typescript'
83
+
84
+ export default class VotingContract extends arc4.Contract {
85
+ topic = GlobalState({ initialValue: 'default_topic', key: Bytes('topic') })
86
+ votes = GlobalState({ initialValue: Uint64(0), key: Bytes('votes') })
87
+ voted = LocalState<uint64>({ key: Bytes('voted') })
88
+
89
+ @arc4.abimethod()
90
+ public setTopic(topic: string): void {
91
+ this.topic.value = topic
92
+ }
93
+ @arc4.abimethod()
94
+ public vote(pay: gtxn.PaymentTxn): boolean {
95
+ assert(op.Global.groupSize === 2, 'Expected 2 transactions')
96
+ assert(pay.amount === 10_000, 'Incorrect payment amount')
97
+ assert(pay.sender === Txn.sender, 'Payment sender must match transaction sender')
98
+
99
+ if (this.voted(Txn.sender).hasValue) {
100
+ return false // Already voted
101
+ }
102
+
103
+ this.votes.value = this.votes.value + 1
104
+ this.voted(Txn.sender).value = 1
105
+ return true
106
+ }
107
+
108
+ @arc4.abimethod({ readonly: true })
109
+ public getVotes(): uint64 {
110
+ return this.votes.value
111
+ }
112
+
113
+ public clearStateProgram(): boolean {
114
+ return true
115
+ }
116
+ }
117
+ ```
118
+
119
+ #### Test Definition
120
+
121
+ ```typescript
122
+ import { Uint64 } from '@algorandfoundation/algorand-typescript'
123
+ import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
124
+ import { afterEach, describe, expect, test } from 'vitest'
125
+ import VotingContract from './contract.algo'
126
+
127
+ describe('Voting contract', () => {
128
+ const ctx = new TestExecutionContext()
129
+ afterEach(() => {
130
+ ctx.reset()
131
+ })
132
+
133
+ test('vote function', () => {
134
+ // Initialize the contract within the testing context
135
+ const contract = ctx.contract.create(VotingContract)
136
+
137
+ const voter = ctx.defaultSender
138
+ const payment = ctx.any.txn.payment({
139
+ sender: voter,
140
+ amount: 10_000,
141
+ })
142
+
143
+ const result = contract.vote(payment)
144
+ expect(result).toEqual(true)
145
+ expect(contract.votes.value).toEqual(1)
146
+ expect(contract.voted(voter).value).toEqual(1)
147
+ })
148
+
149
+ test('setTopic function', () => {
150
+ // Initialize the contract within the testing context
151
+ const contract = ctx.contract.create(VotingContract)
152
+
153
+ const newTopic = ctx.any.string(10)
154
+ contract.setTopic(newTopic)
155
+ expect(contract.topic.value).toEqual(newTopic)
156
+ })
157
+
158
+ test('getVotes function', () => {
159
+ // Initialize the contract within the testing context
160
+ const contract = ctx.contract.create(VotingContract)
161
+
162
+ contract.votes.value = 5
163
+ const votes = contract.getVotes()
164
+ expect(votes).toEqual(5)
165
+ })
166
+ })
167
+ ```
168
+
169
+ This example demonstrates key aspects of testing with `algorand-typescript-testing` for ARC4-based contracts:
170
+
171
+ 1. ARC4 Contract Features:
172
+
173
+ - Use of `arc4.Contract` as the base class for the contract.
174
+ - ABI methods defined using the `@arc4.abimethod` decorator.
175
+ - Readonly method annotation with `@arc4.abimethod({readonly: true})`.
176
+
177
+ 2. Testing ARC4 Contracts:
178
+
179
+ - Creation of an `arc4.Contract` instance within the test context.
180
+ - Use of `ctx.any` for generating random test data.
181
+ - Direct invocation of ABI methods on the contract instance.
182
+
183
+ 3. Transaction Handling:
184
+
185
+ - Use of `ctx.any.txn` to create test transactions.
186
+ - Passing transaction objects as parameters to contract methods.
187
+
188
+ 4. State Verification:
189
+ - Checking global and local state changes after method execution.
190
+ - Verifying return values from ABI methods.
191
+
192
+ > **NOTE**: Thorough testing is crucial in smart contract development due to their immutable nature post-deployment. Comprehensive unit and integration tests ensure contract validity and reliability. Optimizing for efficiency can significantly improve user experience by reducing transaction fees and simplifying interactions. Investing in robust testing and optimization practices is crucial and offers many benefits in the long run.
193
+
194
+ ### Next steps
195
+
196
+ To dig deeper into the capabilities of `algorand-typescript-testing`, continue with the following sections.
197
+
198
+ #### Contents
199
+
200
+ - [Testing Guide](./docs/testing-guide/index.md)
201
+ - [Examples](./docs/examples.md)
202
+ - [Coverage](./docs/coverage.md)
203
+ - [FQA](./docs/faq.md)
204
+ - [API Reference](./docs/api.md)
205
+ - [Algorand TypeScript](./docs/algots.md)
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "**"
5
5
  ],
6
6
  "name": "@algorandfoundation/algorand-typescript-testing",
7
- "version": "1.0.0-beta.16",
7
+ "version": "1.0.0-beta.17",
8
8
  "description": "A library which allows you to execute Algorand TypeScript code locally under a test context either emulating or mocking AVM behaviour.",
9
9
  "private": false,
10
10
  "peerDependencies": {
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "@algorandfoundation/algorand-typescript": "^1.0.0-beta.16",
15
- "@algorandfoundation/puya-ts": "^1.0.0-beta.22",
15
+ "@algorandfoundation/puya-ts": "^1.0.0-beta.23",
16
16
  "elliptic": "^6.5.7",
17
17
  "js-sha256": "^0.11.0",
18
18
  "js-sha3": "^0.9.3",