Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ export default class Reporter {
constructor() {
// Set the Reporter type to realtime.
this.type = 'realtime';
this.cachedTestData = [];
}

// Internal: Creates a websocket connection to the cavy-cli server.
onStart() {
const url = 'ws://127.0.0.1:8082/';
this.ws = new WebSocket(url);
this.ws.onopen = () => {
while (testData = this.cachedTestData.shift()) {
this.sendData(testData);
}
};
}

// Internal: Send a single test result to cavy-cli over the websocket connection.
send(result) {
if (this.websocketReady()) {
testData = { event: 'singleResult', data: result };
const testData = { event: 'singleResult', data: result };
if (this.websocketConnecting()) {
this.cachedTestData.push(testData);
} else if (this.websocketReady()) {
this.sendData(testData);
}
}

// Internal: Send report to cavy-cli over the websocket connection.
onFinish(report) {
if (this.websocketReady()) {
testData = { event: 'testingComplete', data: report };
const testData = { event: 'testingComplete', data: report };
if (this.websocketConnecting()) {
this.cachedTestData.push(testData);
} else if (this.websocketReady()) {
this.sendData(testData);
} else {
// If cavy-cli is not running, let people know in a friendly way
Expand All @@ -35,6 +45,12 @@ export default class Reporter {
}
}

// Private: Determines whether data can not be sent over the websocket yet.
websocketConnecting() {
// WebSocket.readyState 0 means the web socket connection is CONNECTING.
return this.ws.readyState == 0;
}

// Private: Determines whether data can be sent over the websocket.
websocketReady() {
// WebSocket.readyState 1 means the web socket connection is OPEN.
Expand All @@ -49,9 +65,7 @@ export default class Reporter {
console.log('Cavy test report successfully sent to cavy-cli');
}
} catch (e) {
console.group('Error sending test data');
console.warn(e.message);
console.groupEnd();
console.warn(`Error sending test data ${e.message} payload: ${JSON.stringify(testData)}`);
}
}
}