npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

testamenta

v0.1.1

Published

Lightweight, dependency-free test framework with a **Jest-like public API**. Useful for environments where bundlers and Node.js are not required, it allows you to test JavaScript code directly in the browser via a simple `<script>` import.

Downloads

18

Readme

Testamenta

Lightweight, dependency-free test framework with a Jest-like public API. Useful for environments where bundlers and Node.js are not required, it allows you to test JavaScript code directly in the browser via a simple <script> import.

Key Features

  • 📖 Jest-like API: Offers a public interface very similar to Jest (with its obvious limitations), making it intuitive for users already familiar with popular testing frameworks.

  • 📦 No Setup: Requires no installation or package management—simply import the framework using a script tag.

  • ⏳ Async-Aware: Supports asynchronous test execution via async/await.

  • 🔍 Mocking: Built-in support for mock functions and call tracking.

  • 📝 Flexible Logging: Outputs results to the console and the DOM.

Getting Started

Copy the testamenta.js file to your project or import it from a CDN unpkg | jsdelivr.

It can also be installed with npm, but it doesn't make much sense having more complete and robust options like Jest.

// test.html

<script type="module">
  import { tests } from 'https://unpkg.com/testamenta';

  tests(['utils', 'plugins'], {
    path: new URL('.', import.meta.url).href, // path to test files
  });
</script>

Create test files named as *.test.js to be properly loaded. Test files use a syntax similar to Jest.

[!WARNING] The call to describe must be awaited to properly run tests sequentially.

import { describe, it, expect } from 'https://unpkg.com/testamenta';

await describe('Math operations', () => {
  it('should add two numbers', () => {
    expect(1 + 2).toBe(3);
  });
});

Both describe and it can be asynchronous and use async/await, and skipped by using the skip modifier.

import { describe, it, expect } from 'https://unpkg.com/testamenta';

await describe('Promises', () => {
  it('should await for a Promise', async () => {
    const value = await new Promise(resolve => {
      setTimeout(() => resolve(1), 1000);
    });
    expect(value).toBe(1);
  });
});

beforeAll and afterAll can be used to setup and clean up resources before and after each test in a suite. Returned value in beforeAll is passed as parameter to it and afterAll functions.

Mocking functions is supported, allowing you to simulate and track function calls during test execution.

import { describe, it, expect, beforeAll, mockFn } from 'https://unpkg.com/testamenta';

await describe('Mocking', () => {
  const mock = mockFn();

  beforeAll(() => mock.reset());

  it('should mock a function', () => {
    mock(1, 2);
    mock(3, 4);

    expect(mock)
      .toHaveBeenCalledTimes(2);
      .toHaveBeenCalledWith(1, 2);
  });
})

Assertion

The expect function allows for assertions within test cases. It provides a wide range of built-in matchers for testing various conditions:

  • toBe Asserts strict equality.
  • toBeTruthy Asserts that a value is truthy.
  • toBeBoolean, toBeNumber, toBeString, toBeArray, toBeDate, toBeObject, toBeFunction: Type checks for primitives, objects and functions.
  • toHaveLength: Asserts the length of an array or string.
  • toContain: Verifies that an array, object or string contains a specific value.
  • toHaveBeenCalled, toHaveBeenCalledTimes, toHaveBeenCalledWith: Matchers for verifying mock function behavior.

All matchers can be negated with the not modifier.

Multiple assertions can be chained together.

Extend the expect function to add additional custom matchers to increment testing flexibility.

import { describe, it, expect } from 'https://unpkg.com/testamenta';

expect.extend(({ toBeNumber }) => ({
  toBeEven: value => toBeNumber(value) && value % 2 === 0,
}));

await describe('Custom matchers', () => {
  it('should assert even numbers', () => {
    expect(2).toBeEven();
    expect(3).not.toBeEven();
  });
});

Development

To run in Node, execute npm run dev. Test suite will run in terminal in watch mode and will update on each change.

To run in browser, start a Live Server in VSCode.