Skip to content

Commit

Permalink
Refactired out WebSocket shimming
Browse files Browse the repository at this point in the history
  • Loading branch information
jawj committed Dec 18, 2024
1 parent bcf0060 commit 6af67df
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
8 changes: 2 additions & 6 deletions tests/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { expect, test, vi, beforeAll } from 'vitest';
import { neon, neonConfig, Pool } from '../dist/npm';
import { sampleQueries } from './sampleQueries';
import { shimWebSocket } from './ws';

const DB_URL = process.env.VITE_NEON_DB_URL!;
const sql = neon(DB_URL);
const sqlFull = neon(DB_URL, { fullResults: true });
const pool = new Pool({ connectionString: DB_URL });

beforeAll(async () => {
if (typeof WebSocket !== 'function') {
const { WebSocket } = await import('ws');
neonConfig.webSocketConstructor = WebSocket;
}
});
beforeAll(shimWebSocket);

test(
'http query results match WebSocket query results',
Expand Down
8 changes: 2 additions & 6 deletions tests/ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test, beforeAll } from 'vitest';
import { Pool as PgPool } from 'pg';
import * as subtls from 'subtls';
import { sampleQueries } from './sampleQueries';
import { shimWebSocket } from './ws';
import {
neon,
neonConfig,
Expand Down Expand Up @@ -33,12 +34,7 @@ function functionsToPlaceholders(x: any) {
);
}

beforeAll(async () => {
if (typeof WebSocket !== 'function') {
const { WebSocket } = await import('ws');
neonConfig.webSocketConstructor = WebSocket;
}
});
beforeAll(shimWebSocket);

test(
'WebSockets query results match pg/TCP query results, using pool.connect()',
Expand Down
11 changes: 11 additions & 0 deletions tests/ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { neonConfig } from '../dist/npm';

export async function shimWebSocket() {
if (typeof WebSocket !== 'function') {
console.info('Native WebSocket not found: importing ws for neonConfig.webSocketConstructor');
const { WebSocket } = await import('ws');
neonConfig.webSocketConstructor = WebSocket;
} else {
console.info('Using native WebSocket');
}
}

0 comments on commit 6af67df

Please sign in to comment.