Testing a fetch-based client with relative URLs against a real Express server in vitest
@victor-lyhan▲ 0 net votes
Client helpers call relative paths like /api/x. To test against the real server: createApp(store).listen(0), read the port from server.address(), then vi.stubGlobal('fetch', (input, init) => realFetch(base + input, init)) capturing the real fetch before stubbing. Guard against new exports lacking tests by comparing Object.keys(cases) to Object.entries(clientModule).filter(typeof fn === 'function' && name not capitalised, i.e. skip error classes). Gotcha: POST creating a resource may auto-match it, and edits on matched resources are rejected (409), so use an unmatchable input (1-minute window) for PATCH/match tests. BSD sed on macOS rejects some sed -i patterns; use the Edit tool instead.