Skip to content

Commit a7810fe

Browse files
authored
testkit: Enable browser tests log on terminal (#1181)
Introduce a Console message in the protocol between the LocalController on Browser and the RemoteController in Node for enabling the logs and other console messages to be printed in terminal and be available in artifacts. This feature should help debugging the errors in the testing pipeline.
1 parent 71dcb7a commit a7810fe

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

packages/testkit-backend/src/channel/websocket.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export default class WebSocketChannel extends Channel {
1717
if (!this._ws) {
1818
this._ws = new WebSocket(this._adddress)
1919
this._ws.onmessage = ({ data: message }) => {
20-
console.log(message)
21-
console.debug('[WebSocketChannel] Received messsage', message)
20+
console.debug('[WebSocketChannel] Received message', message)
2221
const { messageType, contextId, data } = JSON.parse(message)
2322

2423
switch (messageType) {
@@ -45,12 +44,16 @@ export default class WebSocketChannel extends Channel {
4544
}
4645
}
4746

48-
writeResponse (contextId, response) {
47+
writeResponse (contextId, response, skipLogging) {
4948
if (this._ws) {
50-
console.debug('[WebSocketChannel] Writing response', { contextId, response })
49+
if (!skipLogging) {
50+
console.debug('[WebSocketChannel] Writing response', { contextId, response })
51+
}
5152
return this._ws.send(this._serialize({ contextId, response }))
5253
}
53-
console.error('[WebSocketChannel] Websocket is not connected')
54+
if (!skipLogging) {
55+
console.error('[WebSocketChannel] Websocket is not connected')
56+
}
5457
}
5558

5659
_serialize (val) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { response } from './responses'
2+
3+
const originalConsole = console
4+
5+
export default {
6+
install: (channel) => {
7+
// eslint-disable-next-line no-global-assign
8+
console = new Proxy({}, {
9+
get: (_, method) => (...args) => {
10+
originalConsole[method].apply(originalConsole, args)
11+
channel.writeResponse(null, response('Console', {
12+
method,
13+
args
14+
}), true)
15+
}
16+
})
17+
},
18+
handleConsole: (message) => {
19+
if (message.response.name === 'Console') {
20+
const { method, args } = message.response.data
21+
args[0] = typeof args[0] === 'string' ? `[RemoteConsole] ${args[0]}` : args[0]
22+
console[method].apply(console, args)
23+
return true
24+
}
25+
return false
26+
},
27+
uninstall: () => {
28+
// eslint-disable-next-line no-global-assign
29+
console = originalConsole
30+
}
31+
}

packages/testkit-backend/src/controller/remote.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Controller from './interface'
22
import { WebSocketServer } from 'ws'
33
import { createServer } from 'http'
44
import { HttpStaticServer } from '../infrastructure'
5+
import consoleRemote from '../console.remote'
56

67
/**
78
* RemoteController handles the requests by sending them a remote client.
@@ -81,7 +82,11 @@ export default class RemoteController extends Controller {
8182
this._ws = ws
8283
this._ws.on('message', safeRun(buffer => {
8384
const message = JSON.parse(buffer.toString())
84-
console.debug('[RemoteController] Received messsage', message)
85+
86+
if (consoleRemote.handleConsole(message)) {
87+
return
88+
}
89+
8590
const { contextId, response } = message
8691
this._writeResponse(contextId, response)
8792
}))

packages/testkit-backend/src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getShouldRunTest } from './skipped-tests'
77
import { createGetFeatures } from './feature'
88
import * as REQUEST_HANDLERS from './request-handlers.js'
99
import * as RX_REQUEST_HANDLERS from './request-handlers-rx.js'
10+
import remoteConsole from './console.remote.js'
1011

1112
const SUPPORTED_TLS = (() => {
1213
if (tls.DEFAULT_MAX_VERSION) {
@@ -40,7 +41,9 @@ function main () {
4041

4142
const newChannel = () => {
4243
if (channelType.toUpperCase() === 'WEBSOCKET') {
43-
return new WebSocketChannel(new URL(`ws://localhost:${backendPort}`))
44+
const channel = new WebSocketChannel(new URL(`ws://localhost:${backendPort}`))
45+
remoteConsole.install(channel)
46+
return channel
4447
}
4548
return new SocketChannel(backendPort)
4649
}

packages/testkit-backend/src/responses.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ export function FakeTimeAck () {
160160
return response('FakeTimeAck', {})
161161
}
162162

163-
function response (name, data) {
163+
export function response (name, data) {
164164
return { name, data }
165165
}

0 commit comments

Comments
 (0)