Skip to content

Commit 634a261

Browse files
nvieCTNicholas
andauthored
Dev server usage cleanups (liveblocks#3095)
Co-authored-by: Chris Nicholas <ctnicholasdev@gmail.com>
1 parent 65ed7eb commit 634a261

15 files changed

Lines changed: 73 additions & 198 deletions

File tree

.github/workflows/tests.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@ jobs:
279279

280280
timeout-minutes: 60
281281

282-
env:
283-
LIVEBLOCKS_PUBLIC_KEY: ${{ secrets.E2E_TEST_LIVEBLOCKS_PUBLIC_KEY }}
284-
285282
steps:
286283
- name: Checkout
287284
if: needs.check_for_code_changes.outputs.changes == 'true'
@@ -295,6 +292,10 @@ jobs:
295292
cache: npm
296293
cache-dependency-path: "**/package-lock.json"
297294

295+
- name: Install Bun
296+
if: needs.check_for_code_changes.outputs.changes == 'true'
297+
uses: oven-sh/setup-bun@v1
298+
298299
- name: Install dependencies
299300
if: needs.check_for_code_changes.outputs.changes == 'true'
300301
run: npm install

docs/pages/tools/dev-server.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,45 @@ curl http://localhost:1153/v2/* \
212212
-H 'Authorization: Bearer sk_localdev'
213213
```
214214

215+
## Quick local testing
216+
217+
When your unit tests need the Liveblocks dev server to be running, you can use
218+
`--cmd "vitest run"` (or whichever test command you use) to run your tests in a
219+
temporary, ephemeral, dev server instance.
220+
221+
This mode will:
222+
223+
1. Start a fresh, empty, dev server instance.
224+
2. Run your unit tests.
225+
3. Stop the dev server.
226+
227+
Example output:
228+
229+
```bash
230+
npx liveblocks dev --cmd 'vitest run'
231+
Starting the Liveblocks dev server...
232+
233+
Liveblocks dev server running at http://localhost:1153
234+
Server logs: /tmp/liveblocks-dev-XXXXXX/server.log
235+
236+
✓ src/__tests__/your-unit-test.test.ts (28 tests) 40ms
237+
238+
Test Files 1 passed (1)
239+
Tests 28 passed (28)
240+
Start at 11:24:51
241+
Duration 0.5s
242+
243+
Liveblocks dev server shut down
244+
```
245+
246+
We recommend:
247+
248+
1. `npx liveblocks dev` for manual testing and development, which retains data
249+
between runs in the `.liveblocks/` folder.
250+
2. `npx liveblocks dev --cmd "vitest run"` for locally running your unit tests
251+
against a fresh instance (your local data in `.liveblocks/` won't be affected
252+
by your unit tests).
253+
215254
## Continuous Integration (CI) testing
216255

217256
You can use the dev server in CI environments by setting up a test environment

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/liveblocks-core/e2e/list-property.test.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -335,25 +335,12 @@ test(
335335
throw new Error(
336336
"Consistency violation! Clients disagree on final state of LiveList."
337337
);
338-
} else {
339-
// console.log(
340-
// "---------------------------------------------------"
341-
// );
342-
// console.log("Commands executed:");
343-
// for (const c of commands) {
344-
// console.log(" - ", c.toString());
345-
// }
346-
// console.log("Final consistent state:", JSON.stringify(list1));
347-
// console.log("");
348-
// console.log(
349-
// "---------------------------------------------------"
350-
// );
351338
}
352339
}
353340
),
354341
{
355-
// Run as many tests as you can run in 30 seconds
356-
interruptAfterTimeLimit: 30_000,
342+
// Run as many tests as you can run in 20 seconds
343+
interruptAfterTimeLimit: 20_000,
357344
numRuns: Number.POSITIVE_INFINITY,
358345
verbose: true,
359346
}

packages/liveblocks-core/e2e/utils.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import "dotenv/config";
2-
3-
import fetch from "node-fetch";
4-
import type { URL } from "url";
51
import { expect, onTestFinished } from "vitest";
62
import WebSocket from "ws";
73

@@ -19,9 +15,7 @@ import { mapValues, wait, withTimeout } from "../src/lib/utils";
1915
import type { BaseUserMeta } from "../src/protocol/BaseUserMeta";
2016
import type { Room, RoomEventMessage } from "../src/room";
2117

22-
const BASE_URL =
23-
process.env.NEXT_PUBLIC_LIVEBLOCKS_BASE_URL ?? "https://api.liveblocks.io";
24-
console.log(`Running against Liveblocks base URL: ${BASE_URL}`);
18+
const BASE_URL = "http://localhost:1154";
2519

2620
async function initializeRoomForTest<
2721
P extends JsonObject = JsonObject,
@@ -31,12 +25,6 @@ async function initializeRoomForTest<
3125
TM extends BaseMetadata = BaseMetadata,
3226
CM extends BaseMetadata = BaseMetadata,
3327
>(roomId: string, initialPresence: NoInfr<P>, initialStorage: NoInfr<S>) {
34-
const publicApiKey = process.env.LIVEBLOCKS_PUBLIC_KEY;
35-
36-
if (!publicApiKey) {
37-
throw new Error('Environment variable "LIVEBLOCKS_PUBLIC_KEY" is missing.');
38-
}
39-
4028
let ws: PausableWebSocket | null = null;
4129

4230
class PausableWebSocket extends WebSocket {
@@ -80,11 +68,8 @@ async function initializeRoomForTest<
8068

8169
const client = createClient<U>({
8270
__DANGEROUSLY_disableThrottling: true,
83-
enableDebugLogging: true,
84-
publicApiKey,
71+
publicApiKey: "pk_localdev",
8572
polyfills: {
86-
// @ts-expect-error fetch from Node isn't compatible?
87-
fetch,
8873
WebSocket: PausableWebSocket,
8974
},
9075
baseUrl: BASE_URL,
@@ -193,7 +178,7 @@ export function prepareTestsConflicts<S extends LsonObject>(
193178
// Unfortunately there is no public API to know this has happened. It
194179
// typically happens within ~5 ms, so we'll wait a multitude of that
195180
// here, just to be sure.
196-
setTimeout(resolve, 150);
181+
setTimeout(resolve, 50);
197182
}
198183
beacons.delete(event.beacon);
199184
}
@@ -338,7 +323,7 @@ export function prepareSingleClientTest<S extends LsonObject>(
338323

339324
// Waiting until every messages are received by all clients.
340325
// We don't have a public way to know if everything has been received so we have to rely on time
341-
await wait(600);
326+
await wait(200);
342327

343328
actor.ws.pause();
344329

@@ -354,7 +339,7 @@ export function prepareSingleClientTest<S extends LsonObject>(
354339
actor.ws.resume();
355340
// Waiting until every messages are received by all clients.
356341
// We don't have a public way to know if everything has been received so we have to rely on time
357-
await wait(600);
342+
await wait(200);
358343
},
359344
});
360345
actor.leave();

packages/liveblocks-core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"test:ci": "NODE_OPTIONS=\"--no-deprecation\" vitest run",
4242
"test:types": "ls test-d/* | xargs -n1 tsd --files",
4343
"test:watch": "NODE_OPTIONS=\"--no-deprecation\" vitest",
44-
"test:e2e": "NODE_OPTIONS=\"--no-deprecation\" vitest run --config=./vitest.config.e2e.ts",
44+
"test:e2e": "npx liveblocks dev -p 1154 -c 'vitest run --config=./vitest.config.e2e.ts'",
4545
"test:deps": "depcruise src --exclude __tests__",
4646
"showdeps": "depcruise src --include-only '^src' --exclude='__tests__' --output-type dot | dot -T svg > /tmp/dependency-graph.svg && open /tmp/dependency-graph.svg",
4747
"showdeps:high-level": "depcruise src --include-only '^src' --exclude='(^src/index.ts|shallow.ts|__tests__)' --collapse='^src/(refs|lib|compat|types|crdts|protocol)' --output-type dot | dot -T svg > /tmp/dependency-graph.svg && open /tmp/dependency-graph.svg"
@@ -52,7 +52,6 @@
5252
"@liveblocks/query-parser": "^0.1.1",
5353
"@liveblocks/vitest-config": "*",
5454
"@types/ws": "^8.5.10",
55-
"dotenv": "^16.4.5",
5655
"eslint-plugin-rulesdir": "^0.2.2",
5756
"fast-check": "^4.3.0",
5857
"msw": "^2.10.4",

packages/liveblocks-core/src/client.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ function getBaseUrl(baseUrl?: string | undefined): string {
588588
}
589589
}
590590

591+
function isLocalhost(url: string): boolean {
592+
try {
593+
return new URL(url).hostname === "localhost";
594+
} catch {
595+
return false;
596+
}
597+
}
598+
591599
/**
592600
* Create a client that will be responsible to communicate with liveblocks servers.
593601
*
@@ -617,8 +625,10 @@ export function createClient<U extends BaseUserMeta = DU>(
617625
options: ClientOptions<U>
618626
): Client<U> {
619627
const clientOptions = options;
628+
const baseUrl = getBaseUrl(clientOptions.baseUrl);
620629
const throttleDelay =
621630
process.env.NODE_ENV !== "production" &&
631+
isLocalhost(baseUrl) &&
622632
clientOptions.__DANGEROUSLY_disableThrottling
623633
? 0
624634
: getThrottle(clientOptions.throttle ?? DEFAULT_THROTTLE);
@@ -628,7 +638,6 @@ export function createClient<U extends BaseUserMeta = DU>(
628638
const backgroundKeepAliveTimeout = getBackgroundKeepAliveTimeout(
629639
clientOptions.backgroundKeepAliveTimeout
630640
);
631-
const baseUrl = getBaseUrl(clientOptions.baseUrl);
632641

633642
const currentUserId = new Signal<string | undefined>(undefined);
634643

packages/liveblocks-react-ui/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@
7070
"format": "eslint --fix src/; stylelint --fix src/styles/; prettier --write src/",
7171
"lint": "eslint src/; stylelint src/styles/",
7272
"lint:package": "publint --strict && attw --pack",
73-
"test": "./scripts/run-with-dev-server.sh --port 1154",
74-
"test:ci": "NODE_OPTIONS=\"--no-deprecation\" vitest run",
73+
"test": "npx liveblocks dev -c 'vitest run'",
74+
"test:ci": "vitest run",
7575
"test:types": "ls test-d/* | xargs -n1 tsd --files",
76-
"test:watch": "NODE_OPTIONS=\"--no-deprecation\" vitest"
76+
"test:watch": "vitest"
7777
},
7878
"dependencies": {
7979
"@floating-ui/react-dom": "^2.1.2",

packages/liveblocks-react-ui/scripts/run-with-dev-server.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/liveblocks-redux/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"format": "(eslint --fix src/ || true) && prettier --write src/",
3030
"lint": "eslint src/",
3131
"lint:package": "publint --strict && attw --pack",
32-
"test": "./scripts/run-with-dev-server.sh --port 1154",
33-
"test:ci": "NODE_OPTIONS=\"--no-deprecation\" vitest run",
34-
"test:watch": "NODE_OPTIONS=\"--no-deprecation\" vitest"
32+
"test": "npx liveblocks dev -c 'vitest run'",
33+
"test:ci": "vitest run",
34+
"test:watch": "vitest"
3535
},
3636
"dependencies": {
3737
"@liveblocks/client": "3.14.0",

0 commit comments

Comments
 (0)