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

@phenomnomnominal/to-become

v0.1.2

Published

Jest expect helpers for regression tests where the values may change over time

Downloads

5

Readme

@phenomnomnominal/to-become

npm version

toBecome is a new assertion for Jest that allows you to assert how your code should behave over time.

It's a bit of an experiment that uses Jest snapshot testing to store information about a test that may change.

What does it look like?

import { smaller } from '@phenomnominal/to-become';

import { runLint } from 'linter';

describe('my-thing', () => {
  it('should only ever get smaller', async () => {
    const lintFailures = await runLint();
    expect(lintFailures).toBecome(smaller);
  });
});

Installation

npm install @phenomnomnominal/to-become --save-dev

How does it work?

toBecome enables you to run a special kind of snapshot test. The first time your test runs, toBecome will save a snapshot, just like toMatchSnapshot. If the result of the code-under-test changes on a later test run, a special function (which I'm calling the constraint) will run that compares the actual and the expected value. If the new value still passes that constrain, the snapshot is updated to the new value, and the test will still pass.

Here's a simple example:

import { bigger } from '@phenomnomnominal/to-become';

describe('my-thing', () => {
  it('should only ever get bigger', async () => {
    expect(Date.now()).toBecome(bigger);
  });
});

The first time the test runs, the current timestamp will be saved to the snapshot file.

Each time the test runs in the future, the bigger constraint will run, and compare the value from the snapshot with the new current timestamp. That should always be bigger, so the snapshot will be updated, and the test will pass.

If time was to stop, the test would still pass. The value doesn't have to get bigger, but if it changes, it is only allowed to get bigger.

If time should ever start to go backward, the constraint would fail and the test would fail.

Potential use cases

  • Incrementally reducing lint rule warnings
  • Incrementally reducing type-checking errors
  • Incrementally reducing the number of accessibility audit failures
  • Avoiding performance regressions
  • Other things?

Custom constraints

I haven't come up with any clear use-cases for anything other than bigger or smaller, but they may exist? If you come up with an idea then a) let me know, and b) try out this:

// Import the package to add the `toBecome` assertion to `expect`:
import '@phenomnominal/to-become';

function myConstraint (expected: string, actual: string): boolean {
  // parse values from the snapshot
  const e = JSON.parse(expected);
  const a = JSON.parse(actual);
  // Compare something deeply nested maybe? I dunno.
  return e.foo.bar > a.foo.bar;
}

describe('my-thing', () => {
  it('should change in some expected way', () => {
    expect(something).toBecome(myConstraint);
  });
});