aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/socket/socket.test.ts
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-07 23:45:09 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-07 23:45:43 -0800
commitfadd1c0152ebce389f7d8fa0c297208cd71ff5c0 (patch)
tree3f3eeb5536b725c23744eee6062e2105bc3e0a98 /test/bun.js/socket/socket.test.ts
parent1e19d592739d30d7b1525e2b9102c41f3150e40d (diff)
downloadbun-fadd1c0152ebce389f7d8fa0c297208cd71ff5c0.tar.gz
bun-fadd1c0152ebce389f7d8fa0c297208cd71ff5c0.tar.zst
bun-fadd1c0152ebce389f7d8fa0c297208cd71ff5c0.zip
[Bun.connect] Fix bug where connect() Promise wouldn't reject on a connection error
Diffstat (limited to 'test/bun.js/socket/socket.test.ts')
-rw-r--r--test/bun.js/socket/socket.test.ts70
1 files changed, 58 insertions, 12 deletions
diff --git a/test/bun.js/socket/socket.test.ts b/test/bun.js/socket/socket.test.ts
index 7336306fa..28c945f64 100644
--- a/test/bun.js/socket/socket.test.ts
+++ b/test/bun.js/socket/socket.test.ts
@@ -4,7 +4,7 @@ import { connect, spawn } from "bun";
it("should keep process alive only when active", async () => {
const { exited, stdout, stderr } = spawn({
- cmd: [ bunExe(), "echo.js" ],
+ cmd: [bunExe(), "echo.js"],
cwd: import.meta.dir,
stdout: "pipe",
stdin: null,
@@ -16,16 +16,16 @@ it("should keep process alive only when active", async () => {
expect(await exited).toBe(0);
expect(await new Response(stderr).text()).toBe("");
var lines = (await new Response(stdout).text()).split(/\r?\n/);
- expect(lines.filter(function(line) {
- return line.startsWith("[Server]");
- })).toEqual([
- "[Server] OPENED",
- "[Server] GOT request",
- "[Server] CLOSED",
- ]);
- expect(lines.filter(function(line) {
- return line.startsWith("[Client]");
- })).toEqual([
+ expect(
+ lines.filter(function (line) {
+ return line.startsWith("[Server]");
+ }),
+ ).toEqual(["[Server] OPENED", "[Server] GOT request", "[Server] CLOSED"]);
+ expect(
+ lines.filter(function (line) {
+ return line.startsWith("[Client]");
+ }),
+ ).toEqual([
"[Client] OPENED",
"[Client] GOT response",
"[Client] ENDED",
@@ -33,7 +33,53 @@ it("should keep process alive only when active", async () => {
]);
});
-it("should handle connection error", done => {
+it("should reject on connection error, calling both connectError() and rejecting the promise", (done) => {
+ var data = {};
+ connect({
+ data,
+ hostname: "127.0.0.1",
+ port: 55555,
+ socket: {
+ connectError(socket, error) {
+ expect(socket).toBeDefined();
+ expect(socket.data).toBe(data);
+ expect(error).toBeDefined();
+ expect(error.name).toBe("SystemError");
+ expect(error.message).toBe("Failed to connect");
+ },
+ data() {
+ done(new Error("Unexpected data()"));
+ },
+ drain() {
+ done(new Error("Unexpected drain()"));
+ },
+ close() {
+ done(new Error("Unexpected close()"));
+ },
+ end() {
+ done(new Error("Unexpected end()"));
+ },
+ error() {
+ done(new Error("Unexpected error()"));
+ },
+ open() {
+ done(new Error("Unexpected open()"));
+ },
+ },
+ }).then(
+ () => done(new Error("Promise should reject instead")),
+ (err) => {
+ expect(err).toBeDefined();
+ expect(err.name).toBe("SystemError");
+ expect(err.message).toBe("Failed to connect");
+
+ done();
+ },
+ );
+});
+
+// this also tests we mark the promise as handled if connectError() is called
+it("should handle connection error", (done) => {
var data = {};
connect({
data,