Skip to content

Promise.reject() expects a callback instead of an error (for testing .rejects) #4755

Closed

Description

@franciscop

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

Write the Jest .rejects test example:

test('rejects to octopus', () => {
  // make sure to add a return statement
  return expect(Promise.reject(new Error('octopus'))).rejects.toThrow(
    'octopus',
  );
});

Run bun test

What is the expected behavior?

The test passes with a ✓ rejects to octopus

What do you see instead?

The test fails with this message:

error: Expected value must be a function
✗ remove > rejects to octopus [0.69ms]

Additional information

From the error message it seems the expect() wants a function instead of a promise, but expect(() => Promise.reject(new Error("octopus"))) also doesn't work (Expected promise\nReceived: [Function]).

After some more testing it seems it's Promise.reject() the one with a problem:

// Works (notice function in the reject):
const rejecting = Promise.reject(() => new Error("octopus"));
return expect(rejecting).rejects.toThrow("octopus");

// Doesn't work (notice no function):
const rejecting = Promise.reject(new Error("octopus"));
return expect(rejecting).rejects.toThrow("octopus");

However according to MDN, the correct syntax should be the one without the callback, not the one with the callback:

Promise.reject(new Error('fail'))

Activity

  1. changed the title [-]expect().rejects doesn't work with Jest example[/-] [+]Promise.rejects() expects a callback instead of an error (for testing `.rejects`)[/+] on Sep 10, 2023
  2. changed the title [-]Promise.rejects() expects a callback instead of an error (for testing `.rejects`)[/-] [+]Promise.reject() expects a callback instead of an error (for testing `.rejects`)[/+] on Sep 10, 2023
  3. franciscop commented on Sep 15, 2023

    @franciscop
    Author

    Confirmed as still happening in Bun 1.0.2

  4. self-assigned this
    on May 24, 2024
  5. nektro commented on May 24, 2024

    @nektro
    Contributor

    does this still reproduce for you? it appears to be working on my end in Bun 1.1.9 (you may need to run bun upgrade)

  6. franciscop commented on May 25, 2024

    @franciscop
    Author

    I can confirm this works now! A rejected promise works properly and as expected in JS:

    console.log(Promise.reject(new Error('octopus')));
    // Promise { <rejected> }

    And the default example I put above works. Thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingbun:testSomething related to the `bun test` runner

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions