codi-test-framework
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "codi-test-framework", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A simple test framework for JavaScript", | ||
@@ -19,3 +19,6 @@ "main": "src/testRunner.js", | ||
"author": "Rob Hurst", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"chalk": "^5.3.0" | ||
} | ||
} |
import fs from 'fs'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
export function describe(description, callback) { | ||
console.log(description); | ||
callback(); | ||
console.log(chalk.bold.cyan(`\n${description}`)); | ||
callback(); | ||
} | ||
export function it(description, callback) { | ||
try { | ||
callback(); | ||
console.log(` ✓ ${description}`); | ||
} catch (error) { | ||
console.error(` ✕ ${description}`); | ||
console.error(` ${error.message}`); | ||
} | ||
try { | ||
callback(); | ||
console.log(chalk.green(` ✅ ${description}`)); | ||
} catch (error) { | ||
console.error(chalk.red(` ⛔ ${description}`)); | ||
console.error(chalk.red(` ${error.message}`)); | ||
} | ||
} | ||
@@ -21,34 +22,34 @@ | ||
export function assertEqual(actual, expected, message) { | ||
if (actual !== expected) { | ||
throw new Error(message || `Expected ${actual} to equal ${expected}`); | ||
} | ||
if (actual !== expected) { | ||
throw new Error(message || `Expected ${chalk.bold.yellow(actual)} to equal ${chalk.bold.yellow(expected)}`); | ||
} | ||
} | ||
export function assertNotEqual(actual, expected, message) { | ||
if (actual === expected) { | ||
throw new Error(message || `Expected ${actual} not to equal ${expected}`); | ||
} | ||
if (actual === expected) { | ||
throw new Error(message || `Expected ${chalk.bold.yellow(actual)} not to equal ${chalk.bold.yellow(expected)}`); | ||
} | ||
} | ||
export function assertTrue(actual, message) { | ||
if (actual !== true) { | ||
throw new Error(message || `Expected ${actual} to be true`); | ||
} | ||
if (actual !== true) { | ||
throw new Error(message || `Expected ${chalk.bold.yellow(actual)} to be true`); | ||
} | ||
} | ||
export function assertFalse(actual, message) { | ||
if (actual !== false) { | ||
throw new Error(message || `Expected ${actual} to be false`); | ||
} | ||
if (actual !== false) { | ||
throw new Error(message || `Expected ${chalk.bold.yellow(actual)} to be false`); | ||
} | ||
} | ||
export function assertThrows(callback, errorMessage, message) { | ||
try { | ||
callback(); | ||
throw new Error(message || 'Expected an error to be thrown'); | ||
} catch (error) { | ||
if (error.message !== errorMessage) { | ||
throw new Error(message || `Expected error message to be "${errorMessage}", but got "${error.message}"`); | ||
} | ||
try { | ||
callback(); | ||
throw new Error(message || 'Expected an error to be thrown'); | ||
} catch (error) { | ||
if (error.message !== errorMessage) { | ||
throw new Error(message || `Expected error message to be ${chalk.bold.yellow(errorMessage)}, but got ${chalk.bold.yellow(error.message)}`); | ||
} | ||
} | ||
} | ||
@@ -58,32 +59,40 @@ | ||
async function runTestFile(testFile) { | ||
try { | ||
// Import the test file as an ES module using an absolute path | ||
await import(path.resolve(testFile)); | ||
} catch (error) { | ||
console.error(`Error running test file ${testFile}:`, error); | ||
} | ||
try { | ||
// Import the test file as an ES module using an absolute path | ||
await import(path.resolve(testFile)); | ||
} catch (error) { | ||
console.error(chalk.red(`\nError running test file ${chalk.underline(testFile)}:`)); | ||
console.error(chalk.red(error.stack)); | ||
} | ||
// Function to run all test files in a directory | ||
export function runTests(testDirectory) { | ||
// Read all files in the test directory | ||
const testFiles = fs.readdirSync(testDirectory, { recursive: true }).filter(file => file.endsWith('.mjs')); | ||
// Run each test file | ||
testFiles.forEach(async (file) => { | ||
const testFile = path.join(testDirectory, file); | ||
await runTestFile(testFile); | ||
}); | ||
} | ||
} | ||
// Function to run all test files in a directory | ||
export function runTests(testDirectory) { | ||
// Read all files in the test directory | ||
const testFiles = fs.readdirSync(testDirectory, { recursive: true }).filter(file => file.endsWith('.mjs')); | ||
console.log(chalk.bold.magenta(`\nRunning tests in directory: ${chalk.underline(testDirectory)}`)); | ||
console.log(chalk.bold.magenta(`Found ${testFiles.length} test file(s)\n`)); | ||
// Run each test file | ||
testFiles.forEach(async (file) => { | ||
const testFile = path.join(testDirectory, file); | ||
await runTestFile(testFile); | ||
}); | ||
} | ||
// CLI function | ||
export function runCLI() { | ||
const testDirectory = process.argv[2]; | ||
const testDirectory = process.argv[2]; | ||
if (!testDirectory) { | ||
console.error('Please provide a test directory as an argument.'); | ||
process.exit(1); | ||
} | ||
if (!testDirectory) { | ||
console.error(chalk.red('Please provide a test directory as an argument.')); | ||
process.exit(1); | ||
} | ||
runTests(testDirectory); | ||
console.log(chalk.bold.cyan('='.repeat(40))); | ||
console.log(chalk.bold.cyan('Running tests...')); | ||
console.log(chalk.bold.cyan('='.repeat(40))); | ||
runTests(testDirectory); | ||
} |
4638
114
1
+ Addedchalk@^5.3.0
+ Addedchalk@5.4.1(transitive)