codi-test-framework
Advanced tools
Comparing version 0.0.8 to 0.0.9
{ | ||
"name": "codi-test-framework", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A simple test framework for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "src/testRunner.js", |
# Codi Test Framework ๐ถ | ||
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) | ||
![example workflow](https://github.com/RobAndrewHurst/codi/actions/workflows/unit_tests.yml/badge.svg) | ||
@@ -53,3 +54,2 @@ Codi is a lightweight JavaScript test framework that allows you to write and run tests for your JavaScript code. It provides a simple and intuitive API for defining test suites and test cases, making it easy to ensure the correctness of your code. โจ | ||
- `assertEqual(actual, expected, message)`: Asserts that the actual value is equal to the expected value. โ๏ธ | ||
- `assertDeepEqual(actual, expected, message)`: Asserts that the actual object is equal to the expected object. โ๏ธ | ||
- `assertNotEqual(actual, expected, message)`: Asserts that the actual value is not equal to the expected value. ๐ โโ๏ธ | ||
@@ -56,0 +56,0 @@ - `assertTrue(actual, message)`: Asserts that the actual value is true. โ |
import fs from 'fs'; | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import assertions from './assertions/_assertions.js'; | ||
// Assertion functions | ||
export const assertEqual = assertions.assertEqual; | ||
export const assertNotEqual = assertions.assertNotEqual; | ||
export const assertTrue = assertions.assertTrue; | ||
export const assertFalse = assertions.assertFalse; | ||
export const assertThrows = assertions.assertThrows; | ||
export const assertDeepEqual = assertions.assertDeepEqual; | ||
let passedTests = 0; | ||
@@ -25,70 +34,2 @@ let failedTests = 0; | ||
// Assertion functions | ||
export function assertEqual(actual, expected, message) { | ||
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 ${chalk.bold.yellow(actual)} not to equal ${chalk.bold.yellow(expected)}`); | ||
} | ||
} | ||
export function assertTrue(actual, message) { | ||
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 ${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 ${chalk.bold.yellow(errorMessage)}, but got ${chalk.bold.yellow(error.message)}`); | ||
} | ||
} | ||
} | ||
export function assertDeepEqual(actual, expected, message) { | ||
if (!isDeepEqual(actual, expected)) { | ||
throw new Error(message || `Expected ${chalk.bold.yellow(JSON.stringify(actual))} to deeply equal ${chalk.bold.yellow(JSON.stringify(expected))}`); | ||
} | ||
} | ||
// Helper function to compare objects deeply | ||
function isDeepEqual(obj1, obj2) { | ||
if (obj1 === obj2) { | ||
return true; | ||
} | ||
if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) { | ||
return false; | ||
} | ||
const keys1 = Object.keys(obj1); | ||
const keys2 = Object.keys(obj2); | ||
if (keys1.length !== keys2.length) { | ||
return false; | ||
} | ||
for (const key of keys1) { | ||
if (!keys2.includes(key) || !isDeepEqual(obj1[key], obj2[key])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
// Function to run a single test file | ||
@@ -95,0 +36,0 @@ async function runTestFile(testFile) { |
Sorry, the diff of this file is not supported yet
14344
16
173