From 84fd0cc2b3d21dfc2879966f7e9a0a02a7cc4f58 Mon Sep 17 00:00:00 2001 From: Robert Hurst Date: Tue, 12 Nov 2024 11:04:49 +0200 Subject: [PATCH] only show errors with --quite flag --- example/common.cjs | 9 --------- example/example.mjs | 3 --- src/core/describe.js | 4 +++- src/core/it.js | 5 ++++- src/runners/nodeRunner.js | 32 ++++++++++++++++++-------------- src/state/TestState.js | 7 +++++++ src/testRunner.js | 15 ++++++++++----- tests/example.test.mjs | 5 ----- 8 files changed, 42 insertions(+), 38 deletions(-) delete mode 100644 example/common.cjs delete mode 100644 example/example.mjs diff --git a/example/common.cjs b/example/common.cjs deleted file mode 100644 index 8757ed7..0000000 --- a/example/common.cjs +++ /dev/null @@ -1,9 +0,0 @@ -const fs = require('fs'); - -function helloCommon() { - console.log('Hello world! I just commonJS'); -} - -module.exports = { - helloCommon: helloCommon -}; \ No newline at end of file diff --git a/example/example.mjs b/example/example.mjs deleted file mode 100644 index aba5def..0000000 --- a/example/example.mjs +++ /dev/null @@ -1,3 +0,0 @@ -export function helloworld() { - console.log("Hello World! I am an es6 Module!"); -} \ No newline at end of file diff --git a/src/core/describe.js b/src/core/describe.js index ea2d607..68db1c4 100644 --- a/src/core/describe.js +++ b/src/core/describe.js @@ -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()); diff --git a/src/core/it.js b/src/core/it.js index 5a0ab5a..e22d0a1 100644 --- a/src/core/it.js +++ b/src/core/it.js @@ -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}`)); } diff --git a/src/runners/nodeRunner.js b/src/runners/nodeRunner.js index 0cd594e..6d9a4fe 100644 --- a/src/runners/nodeRunner.js +++ b/src/runners/nodeRunner.js @@ -34,9 +34,10 @@ async function runTestFile(testFile) { * @param {object} [codiConfig={}] - Configuration object * @returns {Promise} 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')); @@ -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.')); diff --git a/src/state/TestState.js b/src/state/TestState.js index 6cf419d..f3007ef 100644 --- a/src/state/TestState.js +++ b/src/state/TestState.js @@ -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; } /** diff --git a/src/testRunner.js b/src/testRunner.js index 298419f..be8dacd 100755 --- a/src/testRunner.js +++ b/src/testRunner.js @@ -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 = {}; @@ -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 }); } \ No newline at end of file diff --git a/tests/example.test.mjs b/tests/example.test.mjs index 4ce93ea..aba02d5 100644 --- a/tests/example.test.mjs +++ b/tests/example.test.mjs @@ -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');