Skip to content

Commit

Permalink
fix(summaryReporter): fix report for nested suites
Browse files Browse the repository at this point in the history
  • Loading branch information
gian1200 committed Jan 16, 2025
1 parent 1abf439 commit d8dbca7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 36 deletions.
24 changes: 16 additions & 8 deletions packages/test-runner-mocha/src/collectTestResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export function collectTestResults(mocha: BrowserMocha) {
...(test.duration !== undefined && { duration: test.duration }),
error: err
? {
name: err.name,
message: err.message,
stack: err.stack,
expected: err.expected,
actual: err.actual,
}
name: err.name,
message: err.message,
stack: err.stack,
expected: err.expected,
actual: err.actual,
}
: undefined,
});
}
Expand All @@ -65,8 +65,16 @@ export function collectTestResults(mocha: BrowserMocha) {

return { name: suite.title, suites, tests };
}

const testResults = getSuiteResults(mocha.suite);
let testResults;
if (mocha.suite.suites.length == 1 && mocha.suite.tests.length == 0) {
collectHooks((mocha.suite as any)._beforeAll as Hook[]);
collectHooks((mocha.suite as any)._afterAll as Hook[]);
collectHooks((mocha.suite as any)._beforeEach as Hook[]);
collectHooks((mocha.suite as any)._afterEach as Hook[]);
testResults = getSuiteResults(mocha.suite.suites[0]);
} else {
testResults = getSuiteResults(mocha.suite);
}

return { testResults, hookErrors, passed };
}
31 changes: 31 additions & 0 deletions packages/test-runner/demo/test/pass-nested-suites.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from './chai.js';
import './shared-a.js';

describe('Root suite test', () => {
it('test 1', () => {
expect(true).to.be.true;
});

it('test 2', () => {
expect('foo').to.be.a('string');
});

describe('Nested suite test', () => {
it('test 3', () => {
expect(true).to.be.true;
});

it('test 4', () => {
expect('foo').to.be.a('string');
});

describe('Nested suite test, level 2', () => {
it('test 5', () => {
expect(true).to.be.true;
});
it('test 6', () => {
expect('foo').to.be.a('string');
});
});
});
});
62 changes: 34 additions & 28 deletions packages/test-runner/src/reporter/summaryReporter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
BrowserLauncher,
Logger,
Reporter,
ReporterArgs,
TestSession,
TestSuiteResult,
} from '@web/test-runner-core';

Expand All @@ -17,8 +17,8 @@ interface Options {

const color =
([x, y]: [number, number]) =>
(z: string) =>
`\x1b[${x}m${z}\x1b[${y}m${reset}`;
(z: string) =>
`\x1b[${x}m${z}\x1b[${y}m${reset}`;
const reset = `\x1b[0m\x1b[0m`;
const green = color([32, 89]);
const red = color([31, 89]);
Expand All @@ -30,54 +30,60 @@ export function summaryReporter(opts: Options): Reporter {
let args: ReporterArgs;
let favoriteBrowser: string;

function log(
function logTest(
logger: Logger,
name: string,
passed: boolean,
skipped: boolean,
prefix = ' ',
prefix: string,
postfix = '',
) {
const sign = skipped ? dim('-') : passed ? green('✓') : red('𐄂');
if (flatten) logger.log(`${sign} ${name}${postfix}`);
else logger.log(`${prefix} ${sign} ${name}`);
else logger.log(`${prefix} ${sign} ${name}`);
}

function logResults(
function logSuite(
logger: Logger,
results?: TestSuiteResult,
prefix?: string,
browser?: BrowserLauncher,
suite: TestSuiteResult,
parent: string,
browserName: string,
) {
const browserName = browser?.name ? ` ${dim(`[${browser.name}]`)}` : '';
for (const result of results?.tests ?? []) {
log(
let prefix = parent ?? '';
if (flatten) prefix += `${suite.name}`;
else logger.log(`${prefix}${suite.name}${!parent ? browserName : ''}`);

for (const test of suite.tests ?? []) {
logTest(
logger,
flatten ? `${prefix ?? ''} ${result.name}` : result.name,
result.passed,
result.skipped,
flatten && prefix ? `${prefix} ${test.name}` : test.name,
test.passed,
test.skipped,
prefix,
browserName,
);
}

for (const suite of results?.suites ?? []) {
logSuite(logger, suite, prefix, browser);
if (!flatten || prefix) prefix += ' '
for (const childSuite of suite.suites ?? []) {
logSuite(logger, childSuite, prefix, browserName);
}
}

function logSuite(
function logResults(
logger: Logger,
suite: TestSuiteResult,
parent?: string,
browser?: BrowserLauncher,
session: TestSession,
) {
const browserName = browser?.name ? ` ${dim(`[${browser.name}]`)}` : '';
let pref = parent ? `${parent} ` : ' ';
if (flatten) pref += `${suite.name}`;
else logger.log(`${pref}${suite.name}${browserName}`);
if (session.testResults) {
const browserName = session.browser?.name ? ` ${dim(`[${session.browser.name}]`)}` : '';
const suite = session.testResults;

logResults(logger, suite, pref, browser);
if (suite.suites.length || suite.tests.length) {
logSuite(logger, suite, '', browserName);
} else {
logger.log(`No tests on ${session.testFile}${browserName}`);
}
}
}

let cachedLogger: Logger;
Expand All @@ -94,7 +100,7 @@ export function summaryReporter(opts: Options): Reporter {
reportTestFileResults({ logger, sessionsForTestFile }) {
cachedLogger = logger;
for (const session of sessionsForTestFile) {
logResults(logger, session.testResults, '', session.browser);
logResults(logger, session);
logger.log('');
}
reportBrowserLogs(logger, sessionsForTestFile);
Expand Down

0 comments on commit d8dbca7

Please sign in to comment.