Skip to content

Commit e60ad64

Browse files
committed
feat(LLD): configure MSW for LLD
1 parent 7608a7c commit e60ad64

File tree

9 files changed

+321
-60
lines changed

9 files changed

+321
-60
lines changed

.changeset/silent-masks-dance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"ledger-live-desktop": minor
33
---
44

5-
Adds fetch mock for MAD/DADA development
5+
Adds MSW to LLD for MAD/DADA development

apps/ledger-live-desktop/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pnpm dev:lld
5454
```
5555

5656
```bash
57-
# launch the app in dev mode with fetch mocking
58-
pnpm dev:lld:mock-fetch
57+
# launch the app in dev mode with MSW
58+
pnpm dev:lld:msw
5959
```
6060

6161
## Watch deps

apps/ledger-live-desktop/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"scripts": {
1818
"start:prod": "electron ./.webpack/main.bundle.js",
1919
"start": "cross-env NODE_ENV=development node ./tools/main.js",
20-
"start:mock-fetch": "cross-env NODE_ENV=development ENABLE_MOCK_FETCH=true node ./tools/main.js",
20+
"start:msw": "cross-env NODE_ENV=development ENABLE_MSW=true node ./tools/main.js",
2121
"build": "cross-env CSC_IDENTITY_AUTO_DISCOVERY=false STAGING=1 node ./tools/dist --nosign -v",
2222
"build:js": "cross-env NODE_ENV=production node ./tools/main.js build",
2323
"build:testing": "cross-env NODE_ENV=production TESTING=1 node ./tools/main.js build",
Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,16 @@
1+
import { setupWorker } from "msw/browser";
2+
import { http, HttpResponse } from "msw";
13
import { mockAssets } from "./dada/mockAssets";
24

3-
interface MockWorker {
4-
start: () => Promise<void>;
5-
stop: () => Promise<void>;
6-
}
5+
const handlers = [
6+
http.get("https://dada.api.ledger-test.com/v1/assets", () => {
7+
return HttpResponse.json(mockAssets);
8+
}),
9+
];
710

8-
let originalFetch: typeof window.fetch;
9-
let isIntercepting = false;
11+
const mswWorker = setupWorker(...handlers);
1012

11-
const mockFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
12-
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
13-
const method = init?.method || "GET";
14-
15-
console.log("Mock Fetch: Intercepting request to:", url);
16-
17-
if (url.includes("https://dada.api.ledger-test.com/v1/assets") && method === "GET") {
18-
return new Response(JSON.stringify(mockAssets), {
19-
status: 200,
20-
headers: { "Content-Type": "application/json" },
21-
});
22-
}
23-
24-
// If no match, resort to original window.fetch
25-
return originalFetch(input, init);
26-
};
27-
28-
export const worker: MockWorker = {
29-
start: async () => {
30-
if (isIntercepting) return;
31-
32-
console.log("Mock Fetch: Starting fetch interception");
33-
originalFetch = window.fetch;
34-
window.fetch = mockFetch;
35-
isIntercepting = true;
36-
},
37-
38-
stop: async () => {
39-
if (!isIntercepting) return;
40-
41-
console.log("Mock Fetch: Stopping fetch interception");
42-
window.fetch = originalFetch;
43-
isIntercepting = false;
44-
},
45-
};
13+
export const startWorker = () =>
14+
mswWorker.start({
15+
onUnhandledRequest: "bypass",
16+
});
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
async function enableMocking() {
2-
const mockFetchEnabled = process.env.ENABLE_MOCK_FETCH === "true";
2+
const mswEnabled = process.env.ENABLE_MSW === "true";
33

4-
if (!mockFetchEnabled) {
4+
if (mswEnabled) {
5+
try {
6+
const { startWorker } = await import("~/mocks/browser");
7+
8+
await startWorker();
9+
} catch (error) {
10+
console.error("MSW: Failed to start Mock Service Worker:", error);
11+
}
512
return;
613
}
714

8-
try {
9-
console.log("Mock Fetch: Starting Mock Service Worker (fetch interceptor)...");
10-
const { worker } = await import("~/mocks/browser");
11-
12-
await worker.start();
13-
14-
console.log("Mock Fetch: Mock Service Worker started successfully");
15-
} catch (error) {
16-
console.error("Mock Fetch: Failed to start Mock Service Worker:", error);
17-
}
15+
const registrations = await navigator.serviceWorker.getRegistrations();
16+
const mswRegistrations = registrations.filter(registration =>
17+
registration.active?.scriptURL.includes("mockServiceWorker"),
18+
);
19+
await Promise.all(
20+
mswRegistrations.map(async registration => {
21+
await registration.unregister();
22+
}),
23+
);
1824
}
1925

2026
enableMocking();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "./pre";
22
import "~/renderer/env";
33
import "~/renderer/experimental"; // NB we need to load this first because it loads things from process.env and will setEnv properly at boot
4-
import "~/mocks/init"; // Initialize Mock Fetch for API mocking
4+
import "~/mocks/init"; // Initialize MSW (Mock Service Worker) for API mocking
55
import "~/renderer/init";

0 commit comments

Comments
 (0)