diff options
| author | 2022-06-22 06:49:42 -0700 | |
|---|---|---|
| committer | 2022-06-22 06:56:47 -0700 | |
| commit | d1ae1771ebf1056187029e643d1b318fe564cc8f (patch) | |
| tree | bc377fb3e449f13aa54fd7f6f8123fb998f9e8ed /integration | |
| parent | 08314c328eac6777edd369c20162ada7f3434535 (diff) | |
| download | bun-d1ae1771ebf1056187029e643d1b318fe564cc8f.tar.gz bun-d1ae1771ebf1056187029e643d1b318fe564cc8f.tar.zst bun-d1ae1771ebf1056187029e643d1b318fe564cc8f.zip | |
Add a simple test for websockets
Diffstat (limited to '')
| -rw-r--r-- | integration/bunjs-only-snippets/websocket.test.js | 39 | 
1 files changed, 39 insertions, 0 deletions
| diff --git a/integration/bunjs-only-snippets/websocket.test.js b/integration/bunjs-only-snippets/websocket.test.js new file mode 100644 index 000000000..37c131591 --- /dev/null +++ b/integration/bunjs-only-snippets/websocket.test.js @@ -0,0 +1,39 @@ +import { describe, it, expect } from "bun:test"; +import { unsafe } from "bun"; + +const TEST_WEBSOCKET_HOST = +  process.env.TEST_WEBSOCKET_HOST || "wss://ws.postman-echo.com/raw"; + +describe("WebSocket", () => { +  it("should connect", async () => { +    const ws = new WebSocket(TEST_WEBSOCKET_HOST); +    await new Promise((resolve, reject) => { +      ws.onopen = resolve; +      ws.onerror = reject; +    }); +    ws.close(); +  }); + +  it("should send and receive messages", async () => { +    const ws = new WebSocket(TEST_WEBSOCKET_HOST); +    await new Promise((resolve, reject) => { +      ws.onopen = resolve; +      ws.onerror = reject; +      ws.onclose = () => { +        expect(ws.bufferedAmount).toBe(0); +        resolve(); +      }; +    }); +    var promise = new Promise((resolve, reject) => { +      ws.onmessage = (event) => { +        expect(event.data).toBe("Hello World!"); +        ws.close(); +        resolve(); +      }; +      ws.onerror = reject; +    }); +    ws.send("Hello World"); + +    await promise; +  }); +}); | 
