Skip to content

Commit

Permalink
fix: do not log websockets when "quiet" is true
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 29, 2024
1 parent 414d47a commit 381d810
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/browser/setupWorker/setupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ export class SetupWorkerApi
getHandlers: () => {
return this.handlersController.currentHandlers()
},
onMockedConnection(connection) {
// Attach the logger for mocked connections since
// those won't be visible in the browser's devtools.
attachLogger(connection)
onMockedConnection: (connection) => {
if (!this.context.startOptions.quiet) {
// Attach the logger for mocked connections since
// those won't be visible in the browser's devtools.
attachLogger(connection)
}
},
onPassthroughConnection() {
/**
Expand Down
34 changes: 34 additions & 0 deletions test/browser/ws-api/ws.logging.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ test.afterAll(async () => {
await server.close()
})

test('does not log anything if "quiet" was set to "true"', async ({
loadExample,
page,
spyOnConsole,
}) => {
const consoleSpy = spyOnConsole()
await loadExample(require.resolve('./ws.runtime.js'), {
skipActivation: true,
})

await page.evaluate(async () => {
const { setupWorker, ws } = window.msw
const api = ws.link('wss://example.com/*')
const worker = setupWorker(api.on('connection', () => {}))
await worker.start({ quiet: true })
})

await page.evaluate(() => {
const ws = new WebSocket('wss://example.com/path')
ws.onopen = () => {
ws.send('hello')
ws.send('world')
queueMicrotask(() => ws.close())
}

return new Promise<void>((resolve, reject) => {
ws.onclose = () => resolve()
ws.onerror = () => reject(new Error('Client connection closed'))
})
})

expect(consoleSpy.get('startGroupCollapsed')).toBeUndefined()
})

test('logs the client connection', async ({
loadExample,
page,
Expand Down

0 comments on commit 381d810

Please sign in to comment.