oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.12k stars 2.67k forks source link

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

Open jacobwheale opened 4 months ago

jacobwheale commented 4 months ago

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.

taorepoara commented 2 weeks ago

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