Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement expect.extend() #3621

Closed
Electroid opened this issue Jul 12, 2023 · 7 comments
Closed

Implement expect.extend() #3621

Electroid opened this issue Jul 12, 2023 · 7 comments
Labels
enhancement New feature or request jest Something related to the `bun test` runner

Comments

@Electroid
Copy link
Contributor

https://jestjs.io/docs/expect#expectextendmatchers

import { expect } from "bun:test";

function toBeWithinRange(actual, floor, ceiling) {
  if (
    typeof actual !== 'number' ||
    typeof floor !== 'number' ||
    typeof ceiling !== 'number'
  ) {
    throw new Error('These must be of type number!');
  }

  const pass = actual >= floor && actual <= ceiling;
  if (pass) {
    return {
      message: () =>
        `expected ${this.utils.printReceived(
          actual,
        )} not to be within range ${this.utils.printExpected(
          `${floor} - ${ceiling}`,
        )}`,
      pass: true,
    };
  } else {
    return {
      message: () =>
        `expected ${this.utils.printReceived(
          actual,
        )} to be within range ${this.utils.printExpected(
          `${floor} - ${ceiling}`,
        )}`,
      pass: false,
    };
  }
}

expect.extend({
  toBeWithinRange,
});
@Electroid Electroid added enhancement New feature or request jest Something related to the `bun test` runner labels Jul 12, 2023
@trnxdev
Copy link
Contributor

trnxdev commented Jul 12, 2023

I can try working on it!

UPD: after working on expect.extend, I realized that it was too hard to me. sorry for the time waste, but I just can't.

@mgasner
Copy link

mgasner commented Jul 13, 2023

fwiw, this is quite important if you're adopting bun (as we would like to do) with a significant base of existing tests that use the react testing library

@Electroid
Copy link
Contributor Author

I can try working on it!

UPD: after working on expect.extend, I realized that it was too hard to me. sorry for the time waste, but I just can't.

It's all good, the Bun team is going to take this one.

@mgasner
Copy link

mgasner commented Sep 11, 2023

fwiw, this is quite important if you're adopting bun (as we would like to do) with a significant base of existing tests that use the react testing library

still!

@GuilhermeMGBR
Copy link

With support to extensions with extends any edge case that would take a while to be prioritized and added to bun could be implemented internally by the team wishing to adopt bun on their workflow.

@fatlard1993
Copy link

Not a perfect fix/workaround by any means, but for those looking for something before official support is added:

You can always use the standard expect assertions to use your matchers, Here's an example using @testing-library/jest-dom/matchers:

import { toBeVisible } from '@testing-library/jest-dom/matchers';
...
expect(toBeVisible(getByRole(container, 'combobox')).pass).toBe(true);

VS

import * as matchers from '@testing-library/jest-dom/matchers';
...
expect.extend(matchers);
...
expect(getByRole(container, 'combobox')).toBeVisible();

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Nov 30, 2023

Implemented by @otgerrogla in #7319

This will ship with Bun v1.0.15, which will either release later tonight or tomorrow.

To upgrade to the canary build:

bun upgrade --canary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request jest Something related to the `bun test` runner
Projects
None yet
Development

No branches or pull requests

6 participants