0.0.11 • Published 5 months ago
@tonkite/jest-tolk v0.0.11
Jest Runner for Tolk
A Jest runner that enables you to write and execute unit tests for Tolk code using familiar testing patterns.
Example
Consider a simple Tolk function in sum.tolk
:
fun calculateSum(a: int, b: int) {
return a + b;
}
You can write unit tests for this function using Tolk:
import "sum.tolk"
// @scope sum()
get test_returns_sum_of_numbers() {
val a: int = 4;
val b: int = 7;
assert(calculateSum(a, b) == a + b) throw 100;
}
// @scope sum()
// @exitCode 7 (type check error)
get test_fails_if_value_is_not_int() {
val a: int = null;
val b: int = 7;
calculateSum(a, b);
}
Test result:
Installation
- Install
@tonkite/jest-tolk
:pnpm add -D @tonkite/jest-tolk
Add runner configuration to
jest.config.ts
:import type { Config } from 'jest'; const config: Config = { projects: [ { displayName: 'test', preset: 'ts-jest', testEnvironment: 'node', testPathIgnorePatterns: ['/node_modules/', '/dist/'], }, { displayName: 'tolk', moduleFileExtensions: ['tolk'], testMatch: ['**/*[._]test.tolk'], runner: '@tonkite/jest-tolk', }, ], }; export default config;
Annotations
The runner allows you to configure the behavior of tests using special annotations in comments.
Example:
/**
* @test
* @scope examples
* @exitCode 500
*/
get should_fail_with_exit_code_500() {
throw 500;
}
Supported Annotations:
Annotation | Example | Description |
---|---|---|
@exitCode [exitCode] | // @exitCode 500 | Specifies the expected exit code. Default: 0 . |
@scope [scope] | // @scope Pool::onSwap | Specifies the scope of a test (useful for test grouping). |
@skip | // @skip | Marks a test to be skipped. |
@todo | // @todo | Marks a test to be done later. |
@test | // @test | Marks a get-method (which isn't prefixed by test_ ) as a test. |
@balance [balance] | // @balance 1000000000 | Sets a balance for a test. Default: 1000000000 (1 TON). |
@gasLimit [gas limit] | // @gasLimit 50000 | Sets a gas limit for a test. Default: 10000 . |
@unixTime [unix time] | // @unixTime 1735231203 | Sets a Unix time for a test. Default: current time. |
@no-main | // @no-main | Disables adding an entrypoint fun main() {} to avoid collision with an existing one. |