Skip to content
New issue

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

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

Already on GitHub? # to your account

Bun test hangs and fails when mocking rejected promises and asserting with Supertest #10437

Open
jacobwheale opened this issue Apr 22, 2024 · 3 comments · May be fixed by #17258
Open

Bun test hangs and fails when mocking rejected promises and asserting with Supertest #10437

jacobwheale opened this issue Apr 22, 2024 · 3 comments · May be fixed by #17258
Labels
bug Something isn't working

Comments

@jacobwheale
Copy link

jacobwheale commented Apr 22, 2024

What version of Bun is running?

1.1.4+fbe2fe0c3

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

When using an Express app and trying to test it with supertest, bun test hangs if a function within the tested express route is mocked to error:

//functions.ts
export const testFunc = async () => {
  setTimeout(() => {}, 200);
  return "Hello from testFunc!";
};
import { test, expect, spyOn } from "bun:test";
import express from "express";
import request from "supertest";
import * as funcs from "./functions";

const app = express();

app.get("/", async (req, res) => {
  try {
    const text = await funcs.testFunc();
    res.status(200);
    return res.send(text);
  } catch (err) {
    res.status(400);
    return res.send("Error");
  }
});

test("supertest", async () => {
  spyOn(funcs, "testFunc").mockRejectedValueOnce(new Error("error"));
  const res = await request(app).get("/").expect(400);

  expect(res.text).toBe("Error");
});

What is the expected behavior?

The test should pass as the function being called inside of the endpoint is being mocked to reject, and the Express route inside of the catch block sets the res.status to 400 and returns "Error".

What do you see instead?

The test hangs and the process doesn't complete:
image

Additional information

This style of tests works using Jest, I have been trying to convert an existing repo from Jest to bun test. if I change the spyOn to mock a different resolved value, the tests do not hang which makes me think it's related to the error being mocked or handling of errors.

@jacobwheale jacobwheale added the bug Something isn't working label Apr 22, 2024
@taorepoara
Copy link

I have the same problem and it seems to work fine when using the watch mode: bun test --watch

@mrcaidev
Copy link

mrcaidev commented Nov 7, 2024

Same problem here. Temporary workaround:

spyOn(funcs, "testFunc").mockImplementationOnce(() => {
  throw new Error("error");
});

@gpfunk
Copy link

gpfunk commented Feb 10, 2025

From what I can tell its related to whether the Promise is handled immediately
If I don't have the fn() call at all, it will fail in the way you've posted above
If I have some delay before calling the fn it will also fail in the way you've posted above

  test.only("mockRejectedValue", async () => {
    const fn = jest.fn();
    fn.mockRejectedValue(new Error(42));

    await new Promise(res => setImmediate(res)); // Fails with this line, passes without it
    await fn().catch(e => null); // Fails if I don't have this line calling and handling the promise

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants