Skip to content

Commit ffbbddf

Browse files
authored
feat: add support for Bun test runner (#28)
1 parent c6602ec commit ffbbddf

File tree

6 files changed

+154
-12
lines changed

6 files changed

+154
-12
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
- Added support for the Bun.js test runner.
12+
13+
### Changed
14+
- Updated `README.md` with examples and instructions for using the Bun matchers.
15+
816
## [0.9.0] - 2025-07-07
917

1018
### Added

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# test-utils
22

3-
This package contains useful testing utilities, such as unit test matchers (for jest and chai) and other useful functions, such as `randomAddress`.
3+
This package contains useful testing utilities, such as unit test matchers (for jest, chai, and bun) and other useful functions, such as `randomAddress`.
44

55
## Installation
66

@@ -14,11 +14,43 @@ npm i --save-dev @ton/test-utils
1414

1515
## Usage
1616

17-
To use the test matchers, just install either jest or chai and import this package like so:
17+
To use the test matchers, just install either jest, chai, or bun and import this package like so:
1818
```typescript
1919
import "@ton/test-utils";
2020
```
2121

22+
### Examples
23+
24+
#### Jest
25+
```typescript
26+
import { expect } from '@jest/globals';
27+
import "@ton/test-utils";
28+
29+
test('cell comparison', () => {
30+
expect(cell1).toEqualCell(cell2);
31+
});
32+
```
33+
34+
#### Chai
35+
```typescript
36+
import { expect } from 'chai';
37+
import "@ton/test-utils";
38+
39+
it('cell comparison', () => {
40+
expect(cell1).to.equalCell(cell2);
41+
});
42+
```
43+
44+
#### Bun
45+
```typescript
46+
import { expect, test } from 'bun:test';
47+
import "@ton/test-utils";
48+
49+
test('cell comparison', () => {
50+
expect(cell1).toEqualCell(cell2);
51+
});
52+
```
53+
2254
### Transaction matcher notice
2355

2456
The transaction matcher (`.toHaveTransaction`) can only perform matching on transactions with descriptions of type `generic`. When matching an array of transactions, all transactions of other types will be filtered out. When matching a single transaction of non-generic type, an exception will be thrown.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@
1717
"@ton/core": "^0.49.2",
1818
"@ton/crypto": "^3.2.0",
1919
"@ton/toolchain": "the-ton-tech/toolchain#v1.4.0",
20+
"@types/bun": "^1.2.19",
2021
"@types/chai": "^4.3.4",
2122
"@types/jest": "^29.4.4",
2223
"chai": "^4.3.7",
2324
"eslint": "^9.28.0",
2425
"jest": "^29.5.0",
2526
"ts-jest": "^29.0.5",
26-
"typescript": "^4.9.5"
27+
"typescript": "^5.9.2"
2728
},
2829
"peerDependencies": {
2930
"@jest/globals": "*",
3031
"@ton/core": ">=0.49.2",
32+
"@types/bun": ">=1.0.0",
3133
"chai": "*"
3234
},
3335
"peerDependenciesMeta": {
3436
"@jest/globals": {
3537
"optional": true
3638
},
39+
"@types/bun": {
40+
"optional": true
41+
},
3742
"chai": {
3843
"optional": true
3944
}
@@ -46,6 +51,7 @@
4651
"lint": "eslint . --max-warnings 0",
4752
"lint:fix": "eslint . --max-warnings 0 --fix",
4853
"test": "jest",
54+
"test:bun": "bun test",
4955
"build": "rm -rf dist && tsc"
5056
},
5157
"packageManager": "yarn@3.6.1"

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export {
1010

1111
import './test/jest';
1212
import './test/chai';
13+
import './test/bun';
1314

1415
export { contractsMeta, ContractsMeta } from './utils/ContractsMeta';
1516

src/test/bun.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import type { MatcherFunction } from 'expect';
2+
import { Address, Cell, Slice } from '@ton/core';
3+
4+
import { FlatTransactionComparable, compareTransactionForTest } from './transaction';
5+
import { CompareResult } from './interface';
6+
import { compareAddressForTest, compareCellForTest, compareSliceForTest } from './comparisons';
7+
import { compareThrownExitCodeForTest } from './exitCode';
8+
9+
function extractResult(result: CompareResult) {
10+
return {
11+
pass: result.pass,
12+
message: () => {
13+
if (result.pass) {
14+
return result.negMessage();
15+
} else {
16+
return result.posMessage();
17+
}
18+
},
19+
};
20+
}
21+
22+
function wrapComparer<T>(
23+
comparer: (subject: unknown, cmp: T) => CompareResult | Promise<CompareResult>,
24+
): MatcherFunction<[cmp: T]> {
25+
return function (actual, cmp) {
26+
const result = comparer(actual, cmp);
27+
if (result instanceof Promise) {
28+
return result.then(extractResult);
29+
}
30+
return extractResult(result);
31+
};
32+
}
33+
34+
const toHaveTransaction = wrapComparer(compareTransactionForTest);
35+
const toEqualCell = wrapComparer(compareCellForTest);
36+
const toEqualAddress = wrapComparer(compareAddressForTest);
37+
const toEqualSlice = wrapComparer(compareSliceForTest);
38+
const toThrowExitCode = wrapComparer(compareThrownExitCodeForTest);
39+
40+
try {
41+
// eslint-disable-next-line @typescript-eslint/no-require-imports
42+
const bunTest = require('bun:test');
43+
44+
if (bunTest && bunTest.expect)
45+
bunTest.expect.extend({
46+
toHaveTransaction,
47+
toEqualCell,
48+
toEqualAddress,
49+
toEqualSlice,
50+
toThrowExitCode,
51+
});
52+
// eslint-disable-next-line no-empty
53+
} catch (_) {}
54+
55+
interface TonMatchers<T> {
56+
toHaveTransaction(cmp: FlatTransactionComparable): T;
57+
toEqualCell(cell: Cell): T;
58+
toEqualAddress(address: Address): T;
59+
toEqualSlice(slice: Slice): T;
60+
61+
/**
62+
* @example
63+
* await expect(() => blockchain.runGetMethod(contract.address, 'test')).toThrowExitCode(11);
64+
*/
65+
toThrowExitCode(exitCode: number): Promise<T>;
66+
}
67+
68+
declare module 'bun:test' {
69+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
70+
interface Matchers<T> extends TonMatchers<T> {}
71+
}

yarn.lock

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,21 +1015,25 @@ __metadata:
10151015
"@ton/core": ^0.49.2
10161016
"@ton/crypto": ^3.2.0
10171017
"@ton/toolchain": "the-ton-tech/toolchain#v1.4.0"
1018+
"@types/bun": ^1.2.19
10181019
"@types/chai": ^4.3.4
10191020
"@types/jest": ^29.4.4
10201021
chai: ^4.3.7
10211022
eslint: ^9.28.0
10221023
jest: ^29.5.0
10231024
node-inspect-extracted: ^2.0.0
10241025
ts-jest: ^29.0.5
1025-
typescript: ^4.9.5
1026+
typescript: ^5.9.2
10261027
peerDependencies:
10271028
"@jest/globals": "*"
10281029
"@ton/core": ">=0.49.2"
1030+
"@types/bun": ">=1.0.0"
10291031
chai: "*"
10301032
peerDependenciesMeta:
10311033
"@jest/globals":
10321034
optional: true
1035+
"@types/bun":
1036+
optional: true
10331037
chai:
10341038
optional: true
10351039
languageName: unknown
@@ -1100,6 +1104,15 @@ __metadata:
11001104
languageName: node
11011105
linkType: hard
11021106

1107+
"@types/bun@npm:^1.2.19":
1108+
version: 1.2.19
1109+
resolution: "@types/bun@npm:1.2.19"
1110+
dependencies:
1111+
bun-types: 1.2.19
1112+
checksum: 175d73e404bd5ee336ce2e392c2632195938d8258692193161df5b865c665b0e835521f144d12d450519349ba14e2821bf0e142d8714cb3886f4f9f0672af87a
1113+
languageName: node
1114+
linkType: hard
1115+
11031116
"@types/chai@npm:^4.3.4":
11041117
version: 4.3.4
11051118
resolution: "@types/chai@npm:4.3.4"
@@ -1772,6 +1785,17 @@ __metadata:
17721785
languageName: node
17731786
linkType: hard
17741787

1788+
"bun-types@npm:1.2.19":
1789+
version: 1.2.19
1790+
resolution: "bun-types@npm:1.2.19"
1791+
dependencies:
1792+
"@types/node": "*"
1793+
peerDependencies:
1794+
"@types/react": ^19
1795+
checksum: 646e2cb9a74ffbd2b103324d215da2db998a8075b512a3847acceb7042ffa0c9f66288a536af3788d44cc4ca54b2ba2292bb2f034c875d1f7efe1e18ff20c11b
1796+
languageName: node
1797+
linkType: hard
1798+
17751799
"cacache@npm:^17.0.0":
17761800
version: 17.1.3
17771801
resolution: "cacache@npm:17.1.3"
@@ -5834,23 +5858,23 @@ __metadata:
58345858
languageName: node
58355859
linkType: hard
58365860

5837-
"typescript@npm:^4.9.5":
5838-
version: 4.9.5
5839-
resolution: "typescript@npm:4.9.5"
5861+
"typescript@npm:^5.9.2":
5862+
version: 5.9.2
5863+
resolution: "typescript@npm:5.9.2"
58405864
bin:
58415865
tsc: bin/tsc
58425866
tsserver: bin/tsserver
5843-
checksum: ee000bc26848147ad423b581bd250075662a354d84f0e06eb76d3b892328d8d4440b7487b5a83e851b12b255f55d71835b008a66cbf8f255a11e4400159237db
5867+
checksum: f619cf6773cfe31409279711afd68cdf0859780006c50bc2a7a0c3227f85dea89a3b97248846326f3a17dad72ea90ec27cf61a8387772c680b2252fd02d8497b
58445868
languageName: node
58455869
linkType: hard
58465870

5847-
"typescript@patch:typescript@^4.9.5#~builtin<compat/typescript>":
5848-
version: 4.9.5
5849-
resolution: "typescript@patch:typescript@npm%3A4.9.5#~builtin<compat/typescript>::version=4.9.5&hash=289587"
5871+
"typescript@patch:typescript@^5.9.2#~builtin<compat/typescript>":
5872+
version: 5.9.2
5873+
resolution: "typescript@patch:typescript@npm%3A5.9.2#~builtin<compat/typescript>::version=5.9.2&hash=14eedb"
58505874
bin:
58515875
tsc: bin/tsc
58525876
tsserver: bin/tsserver
5853-
checksum: 1f8f3b6aaea19f0f67cba79057674ba580438a7db55057eb89cc06950483c5d632115c14077f6663ea76fd09fce3c190e6414bb98582ec80aa5a4eaf345d5b68
5877+
checksum: e42a701947325500008334622321a6ad073f842f5e7d5e7b588a6346b31fdf51d56082b9ce5cef24312ecd3e48d6c0d4d44da7555f65e2feec18cf62ec540385
58545878
languageName: node
58555879
linkType: hard
58565880

0 commit comments

Comments
 (0)