Skip to content

Commit

Permalink
Custom reporter cannot also have an output file, he finds out two day…
Browse files Browse the repository at this point in the history
…s late
  • Loading branch information
philrenaud committed Nov 28, 2024
1 parent bf42fdb commit f98bd61
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
28 changes: 17 additions & 11 deletions ui/test-reporter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

/* eslint-env node */
/* eslint-disable no-console */

Expand All @@ -6,22 +11,16 @@ const path = require('path');

class JsonReporter {
constructor(out, socket, config) {
// Prevent double initialization
if (JsonReporter.instance) {
return JsonReporter.instance;
}
JsonReporter.instance = this;

this.out = out || process.stdout;
this.results = [];

// Get output file from Testem config
this.outputFile = config.fileOptions.report_file || 'test-results.json';
this.outputFile =
config.fileOptions.custom_report_file || 'test-results/test-results.json';

console.log(`[Reporter] Initializing with output file: ${this.outputFile}`);

try {
// Ensure output directory exists
fs.mkdirSync(path.dirname(this.outputFile), { recursive: true });

// Initialize the results file
Expand All @@ -47,6 +46,9 @@ class JsonReporter {
this.finish();
process.exit(0);
});

this.testCounter = 0;
this.startTime = Date.now();
}

filterLogs(logs) {
Expand All @@ -55,6 +57,7 @@ class JsonReporter {
if (
log.text &&
(log.text.includes('Accessor:') ||
log.text.includes('log in with a JWT') ||
log.text === 'TOKENS:' ||
log.text === '=====================================')
) {
Expand All @@ -72,7 +75,8 @@ class JsonReporter {
return;
}

console.log(`[Reporter] Processing test: ${data.name}`);
this.testCounter++;
console.log(`[Reporter] Test #${this.testCounter}: ${data.name}`);

const partitionMatch = data.name.match(/^Exam Partition (\d+) - (.*)/);

Expand Down Expand Up @@ -103,10 +107,12 @@ class JsonReporter {
const passed = this.results.filter((r) => r.passed).length;
const failed = this.results.filter((r) => !r.passed).length;
const total = this.results.length;
const duration = Date.now() - this.startTime;

const output = {
summary: { total, passed, failed },
timestamp: new Date().toISOString(),
duration,
tests: this.results,
};

Expand All @@ -117,15 +123,15 @@ class JsonReporter {
console.log(`Total: ${total}`);
console.log(`Passed: ${passed}`);
console.log(`Failed: ${failed}`);

console.log(`Duration: ${duration}ms`);
if (failed > 0) {
console.log('\nFailed Tests:');
this.results
.filter((r) => !r.passed)
.forEach((r) => {
console.log(`❌ ${r.name}`);
if (r.error) {
console.log(` ${r.error}`);
console.error(r.error);
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion ui/testem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const config = {
parallel: -1,
framework: 'qunit',
reporter: JsonReporter,
report_file: 'test-results/test-results.json',
custom_report_file: 'test-results/test-results.json',
// report_file: 'test-results/test-results.json',
// NOTE: See https://github.com/testem/testem/issues/1073, report_file + custom reporter results in double output.
debug: true,

browser_args: {
// New format in testem/master, but not in a release yet
// Chrome: {
Expand Down
2 changes: 1 addition & 1 deletion ui/tests/acceptance/variables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ module('Acceptance | variables', function (hooks) {
assert.ok(confirmFired, 'Confirm fired when leaving with unsaved form');
assert.equal(
currentURL(),
'/variables/var/Auto-conflicting%20Variable@default/edit',
'/variables/var/Auto-conflicting%20Variable@default/editte',
'Opted to stay, did not leave page'
);

Expand Down

0 comments on commit f98bd61

Please sign in to comment.