ospec: Noiseless Testing Framework

4.2.1 · active · verified Sun Apr 19

ospec is a lightweight and "noiseless" JavaScript testing framework, currently at version 4.2.1. It aims for terser and faster test code compared to alternatives like Mocha, Jasmine, or Tape, emphasizing readability through its Subject-Verb-Object assertion structure. The framework includes features for test grouping, asynchronous tests, `before/after` hooks, test exclusivity (`.only`), and spies for mocking functions. Its core design principle is to regulate test-space configuration to promote focus on testing logic and encourage uniform test suites. The project maintains a small footprint, aiming for around 800 lines of code after a planned API surface change, with a current release cadence that reflects ongoing development and refinements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic test definition, grouping, nested groups, assertions, spy usage, and asynchronous testing with ospec.

import o from 'ospec';

// Basic test and assertions
o.spec("math operations", function() {
    o("addition works", function() {
        o(1 + 1).equals(2)("basic addition assertion");
        o(2 + 2).equals(4);
    });

    o("subtraction works", function() {
        o(1 - 1).notEquals(2)("basic subtraction assertion");
    });

    // Nested test groups
    o.spec("arithmetics details", function() {
        o("multiplication also works", function() {
            o(2 * 3).equals(6);
        });
    });
});

// Example with o.spy for function call tracking
function executeCallback(cb, arg) {
    cb(arg);
}

o.spec("executeCallback()", function() {
    o("calls the provided callback with the argument", function() {
        var spy = o.spy();
        executeCallback(spy, "test-argument");

        o(spy.callCount).equals(1)("spy should be called once");
        o(spy.args[0]).equals("test-argument")("first call argument should match");
        o(spy.calls[0].args).deepEquals(["test-argument"])("full call arguments should deep equal");
    });
});

// To run the tests, typically you would run this file via the `ospec` CLI or call o.run()
// Assuming a simple setup where tests are executed on file load, 
// or for explicit execution, the 'ospec' runner needs to be invoked. 
// For demonstration, we simulate running all defined tests.
// In a real CLI setup, this explicit `o.run()` might not be needed.
// However, if running directly in Node, it might be. Let's omit for general quickstart.
// A typical setup would involve `npx ospec my-tests.js`

// Example of an async test
o.spec("async operations", function() {
    o("handles promises correctly", function(done) {
        Promise.resolve(5).then(result => {
            o(result).equals(5);
            done(); // Important for async tests to signal completion
        });
    });
});

view raw JSON →