test.js Unit Test Suite
test.js is an extremely lightweight, console API-based unit testing library designed for both Node.js and browser environments. It provides a minimal API for defining test suites and individual tests using simple `[EXPRESSION, EXPECTATION]` arrays. The library aims for simplicity over feature richness, distinguishing itself from more comprehensive testing frameworks. Its latest version, 0.0.4, was released in 2014, indicating that the project is no longer actively maintained. Due to its age, it supports CommonJS modules and global browser scope but lacks modern JavaScript features like ESM, async/await testing, or advanced assertion libraries. Developers seeking a simple, low-overhead solution for basic equality checks in legacy projects might find it useful, but it is not recommended for new projects or environments requiring modern testing paradigms or comprehensive reporting.
Common errors
-
ReferenceError: test is not defined
cause Attempting to use 'test' in a browser environment without including the <script src="test.js"></script> tag, or before the script has fully loaded.fixEnsure 'test.js' is loaded via a script tag before calling any 'test' methods in the browser. Place your test script after the library script. -
ReferenceError: require is not defined
cause Attempting to use CommonJS 'require' in a browser environment, an ES Module context, or without a Node.js-like environment.fixIn a Node.js CommonJS environment, ensure 'var test = require('test-js');' is at the top of your file. In a browser, use the global 'test' object after loading the script via <script> tag. This library does not support ES Modules. -
TypeError: test.suite is not a function
cause The 'test' object was not correctly loaded, or a different global variable named 'test' is overshadowing the library's export.fixVerify the correct import/script loading method for your environment (CommonJS 'require' for Node.js, <script> tag for browser). Check for conflicting global variables if in a browser environment.
Warnings
- breaking Project is abandoned. The last release (0.0.4) was in 2014, and there is no active development or maintenance. Users should not expect bug fixes, security updates, or new features.
- gotcha No support for modern JavaScript features. test.js predates ES Modules, async/await, and modern assertion libraries, leading to verbose or impossible testing patterns for contemporary codebases.
- gotcha Limited assertion capabilities. The framework only supports simple equality checks with [EXPRESSION, EXPECTATION] pairs, lacking advanced assertions (e.g., toContain, toMatchObject, toThrow) or mock/spy functionality.
Install
-
npm install test-js -
yarn add test-js -
pnpm add test-js
Imports
- test
import test from 'test-js';
var test = require('test-js'); - test.suite
const suite = test.suite; suite('My Suite', { /* tests */ });test.suite('My Suite', { /* tests */ }); - test (global)
import { test } from './test.js';<script src="test.js"></script> <script> test.suite('My Suite', { /* tests */ }); </script>
Quickstart
// quickstart.js
// This example demonstrates basic usage of the test.js library for simple unit tests.
// Run this file with Node.js after 'npm install test-js' or include test.js in an HTML file.
var test = require('test-js'); // For Node.js environments
// Define a test suite named 'Basic Arithmetic Operations'
test.suite('Basic Arithmetic Operations', {
// A simple test for addition
'Addition: 1 + 1 should equal 2': [1 + 1, 2],
// Another addition test, expecting a different result
'Addition: 5 + 3 should equal 8': [5 + 3, 8],
// Test for subtraction
'Subtraction: 10 - 5 should equal 5': [10 - 5, 5],
// An intentionally failing test to demonstrate failure output
'Subtraction: 7 - 2 should NOT equal 4 (expected to fail)': [7 - 2, 4], // This will fail
// Test for multiplication
'Multiplication: 3 * 4 should equal 12': [3 * 4, 12],
// Test for division
'Division: 10 / 2 should equal 5': [10 / 2, 5]
});
// Define another suite for boolean logic
test.suite('Boolean Logic Tests', {
'True is true': [true, true],
'False is false': [false, false],
'NOT true is false': [!true, false],
'AND operator: true && true is true': [true && true, true],
'OR operator: false || true is true': [false || true, true]
});
// To run this in Node.js:
// 1. Save as e.g., 'my-tests.js'
// 2. npm install test-js
// 3. node my-tests.js
//
// To run in a browser:
// 1. Download test.js
// 2. <script src="test.js"></script>
// 3. <script src="my-tests.js"></script>
// 4. Open the HTML file in a browser and check the console.