Skip to content

Commit

Permalink
only show errors with --quite flag
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Nov 12, 2024
1 parent abd002e commit 84fd0cc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 38 deletions.
9 changes: 0 additions & 9 deletions example/common.cjs

This file was deleted.

3 changes: 0 additions & 3 deletions example/example.mjs

This file was deleted.

4 changes: 3 additions & 1 deletion src/core/describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export async function describe(description, callback) {
};

state.pushSuite(suite);
console.log(chalk.bold.cyan(`\n${description}`));
if (!state.options?.quiet) {
console.log(chalk.bold.cyan(`\n${description}`));
}

try {
await Promise.resolve(callback());
Expand Down
5 changes: 4 additions & 1 deletion src/core/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ export async function it(description, callback) {
test.status = 'passed';
test.duration = performance.now() - test.startTime;
state.passedTests++;
console.log(chalk.green(` ✅ ${description} (${test.duration.toFixed(2)}ms)`));
if (!state.options?.quiet) {
console.log(chalk.green(` ✅ ${description} (${test.duration.toFixed(2)}ms)`));
}
} catch (error) {
test.status = 'failed';
test.error = error;
test.duration = performance.now() - test.startTime;
state.failedTests++;
// Always show errors, even in quiet mode
console.error(chalk.red(` ⛔ ${description} (${test.duration.toFixed(2)}ms)`));
console.error(chalk.red(` ${error.message}`));
}
Expand Down
32 changes: 18 additions & 14 deletions src/runners/nodeRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ async function runTestFile(testFile) {
* @param {object} [codiConfig={}] - Configuration object
* @returns {Promise<object|void>} Test results if returnResults is true
*/
export async function runTests(testDirectory, returnResults = false, codiConfig = {}) {
export async function runTests(testDirectory, returnResults = false, codiConfig = {}, options = {}) {
state.resetCounters();
state.startTimer();
state.setOptions(options);

let testFiles = fs.readdirSync(testDirectory, { recursive: true })
.filter(file => file.endsWith('.mjs'));
Expand All @@ -46,26 +47,29 @@ export async function runTests(testDirectory, returnResults = false, codiConfig
testFiles = testFiles.filter(file => !matcher(file));
}

console.log(chalk.bold.magenta(`\nRunning tests in directory: ${chalk.underline(testDirectory)}`));
console.log(chalk.bold.magenta(`Found ${testFiles.length} test file(s)\n`));
if (!options.quiet) {
console.log(chalk.bold.magenta(`\nRunning tests in directory: ${chalk.underline(testDirectory)}`));
console.log(chalk.bold.magenta(`Found ${testFiles.length} test file(s)\n`));
}

for (const file of testFiles) {
await runTestFile(path.join(testDirectory, file));
}

const summary = {
passedTests: state.passedTests,
failedTests: state.failedTests,
testResults: state.testResults,
executionTime: state.getExecutionTime()
};

// Always show the final summary
console.log(chalk.bold.cyan('\nTest Summary:'));
console.log(chalk.green(` Passed: ${summary.passedTests}`));
console.log(chalk.red(` Failed: ${summary.failedTests}`));
console.log(chalk.blue(` Time: ${summary.executionTime}s`));
console.log(chalk.green(` Passed: ${state.passedTests}`));
console.log(chalk.red(` Failed: ${state.failedTests}`));
console.log(chalk.blue(` Time: ${state.getExecutionTime()}s`));

if (returnResults) return summary;
if (returnResults) {
return {
passedTests: state.passedTests,
failedTests: state.failedTests,
testResults: state.testResults,
executionTime: state.getExecutionTime()
};
}

if (state.failedTests > 0) {
console.log(chalk.red('\nSome tests failed.'));
Expand Down
7 changes: 7 additions & 0 deletions src/state/TestState.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class TestState {

/** @type {number|null} Test start time */
this.startTime = null;

/** @type {object} options */
this.options = {};
}

setOptions(options) {
this.options = options;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function runCLI() {
const returnResults = process.argv.includes('--returnResults');
const returnVersion = process.argv.includes('--version');
const configPathIndex = process.argv.indexOf('--config');
const quiet = process.argv.includes('--quiet');

let codiConfig = {};

Expand All @@ -64,12 +65,16 @@ export async function runCLI() {
if (err.code !== 'ENOENT') {
throw err;
}
console.log(chalk.yellow(`No config file found at ${configPath}, proceeding with default settings`));
if (!quiet) {
console.log(chalk.yellow(`No config file found at ${configPath}, proceeding with default settings`));
}
}

console.log(chalk.bold.cyan('='.repeat(40)));
console.log(chalk.bold.cyan('Running tests...'));
console.log(chalk.bold.cyan('='.repeat(40)));
if (!quiet) {
console.log(chalk.bold.cyan('='.repeat(40)));
console.log(chalk.bold.cyan('Running tests...'));
console.log(chalk.bold.cyan('='.repeat(40)));
}

await nodeRunTests(testDirectory, returnResults, codiConfig);
await nodeRunTests(testDirectory, returnResults, codiConfig, { quiet });
}
5 changes: 0 additions & 5 deletions tests/example.test.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { describe, it, assertEqual, assertNotEqual, assertTrue, assertFalse, assertThrows, assertNoDuplicates, runTestFunction } from '../src/testRunner.js';
import { helloworld } from '../example/example.mjs';
import pkg from '../example/common.cjs';
const { helloCommon } = pkg;

// First test suite
await describe('I am an Example Test Suite', () => {
helloworld();
helloCommon();

it('should pass equality assertion', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
Expand Down

0 comments on commit 84fd0cc

Please sign in to comment.