Codi Test Framework πΆ

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. β¨
Features β
- π Simple and expressive API for writing tests
- ποΈ Supports test suites and test cases
- π Provides assertion functions for comparing expected and actual values
- π Colorful output for better readability
- π₯οΈ Supports running tests from the command line
- π Compatible with ECMAScript modules (ESM)
- β‘ Super fast thanks to bun.sh
Installation π¦
To use Codi in your project, you need to have Node.js installed. You can install Codi as a development dependency using npm:
npm install --save-dev codi-test-framework
For user using codi v0.0.14^ ensure that you have bun v1.1.0 installed.
Usage π οΈ
Writing Tests βοΈ
To write tests using Codi, create a new test file with a .mjs extension. Use the describe function to define a test suite and the it function to define individual test cases.
Here's an example test file:
import { describe, it, assertEqual } from 'codi-test-framework';
describe('Math operations', () => {
it('should add two numbers correctly', () => {
const result = 2 + 3;
assertEqual(result, 5, 'Addition should work correctly');
});
it('should subtract two numbers correctly', () => {
const result = 5 - 3;
assertEqual(result, 2, 'Subtraction should work correctly');
});
});
Assertion Functions π§ͺ
Codi provides several assertion functions to compare expected and actual values:
assertEqual(actual, expected, message): Asserts that the actual value is equal to the expected value. βοΈ
assertNotEqual(actual, expected, message): Asserts that the actual value is not equal to the expected value. π
ββοΈ
assertTrue(actual, message): Asserts that the actual value is true. β
assertFalse(actual, message): Asserts that the actual value is false. β
assertThrows(callback, errorMessage, message): Asserts that the provided callback function throws an error with the specified error message. π₯
Running Tests πββοΈ
To run the tests, use the runTests function and provide the directory containing your test files:
import { runTests } from 'codi-test-framework';
runTests('./tests');
You can also run the tests from the command line using the runCLI function:
codi ./tests
Browser Testing π
Codi supports running tests in a headless browser environment, perfect for testing DOM manipulation, browser APIs, and web-specific functionality.
To run tests in a browser environment:
codi ./tests --browser
Browser tests have access to:
window and document objects
- DOM APIs (createElement, querySelector, etc.)
- Browser APIs (localStorage, fetch, etc.)
- All standard Codi assertion functions
Example browser test:
import { describe, it, assertEqual, assertTrue } from 'codi-test-framework';
describe({ name: 'DOM Tests', id: 'dom_tests' }, () => {
it({ name: 'should create DOM elements', parentId: 'dom_tests' }, () => {
const div = document.createElement('div');
div.textContent = 'Hello World';
assertEqual(div.tagName, 'DIV');
assertTrue(div.textContent === 'Hello World');
});
});
The browser runner uses Puppeteer under the hood and works great in CI environments like GitHub Actions.
License π
This project is licensed under the MIT License.
Feel free to contribute to Codi by opening issues or submitting pull requests. Happy testing! π