Skip to content

Enable Ton matchers in TypeScript when using expect from @jest/globals#31

Merged
krigga merged 1 commit intoton-org:developfrom
EhsanMarufi:feature/jest-typings-augment-imported-expect
Aug 4, 2025
Merged

Enable Ton matchers in TypeScript when using expect from @jest/globals#31
krigga merged 1 commit intoton-org:developfrom
EhsanMarufi:feature/jest-typings-augment-imported-expect

Conversation

@EhsanMarufi
Copy link
Copy Markdown
Contributor

@EhsanMarufi EhsanMarufi commented Jun 26, 2025

Ton custom matchers like toHaveTransaction were previously only available when using the global expect. This PR adds full TypeScript support for projects that import expect explicitly from @jest/globals.

Changes

  • Introduced a TonMatchers<R> interface to avoid duplication.
  • Augmented both:
    • Global jest.Matchers<R> (for traditional global usage).
    • Module @jest/expect's Matchers<R> (for explicit expect imports).
  • Ensured module augmentation is triggered by importing 'expect'.
  • No runtime changes; this is a pure typings enhancement.

Motivation

Modern Jest usage, especially in TypeScript projects, often relies on explicit imports like:

import { expect } from '@jest/globals';

Without this update, consumers lose type safety for Ton-specific matchers unless they use the global expect, which can lead to confusion or unnecessary workarounds.

Benefits

  • Enables proper typings for Ton matchers when using expect via @jest/globals.
  • Improves developer experience and consistency across test styles.
  • Avoids the need for local patches, manual module augmentation, or as any casts.

Checklist

  • All tests pass successfully (yarn test)
  • Code passes linting checks (yarn lint)

…est/globals

Ton custom matchers like `toHaveTransaction` were previously only available when using the global `expect`. This commit adds proper TypeScript support for projects that explicitly import `expect` from `@jest/globals`.

- Extracted shared matcher definitions into `TonMatchers<R>`.
- Augmented both global and module `Matchers` interfaces.

Enables type-safe use of Ton matchers with imported `expect`, improving developer experience and aligning with modern Jest usage patterns.
@archseer
Copy link
Copy Markdown

archseer commented Jul 11, 2025

I also used something like this:

function tonEquals(a, b) {
  if (a instanceof Address) {
      if (!(b instanceof Address)) return false;
      return a.equals(b);
  }

  if (a instanceof Cell) {
      if (!(b instanceof Cell)) return false;
      return a.equals(b);
  }

  return undefined
}

// Extend expect to support Address and Cell equality
import {expect} from '@jest/globals';
expect.addEqualityTesters([tonEquals]);

This way Jest's matchers will use TON specific matches on recursive matchers too, e.g. toMatchObject

    expect(msg).toMatchObject({ sender: deployer.address })

@krigga krigga merged commit c6602ec into ton-org:develop Aug 4, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants