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

jest-t-assert

v0.3.0

Published

Use tape style assertions with Jest

Downloads

299

Readme

jest-t-assert

Build Status npm

Use tape style assertions with Jest. The assertions are very similar to AVA.

If you just want to migrate from AVA to Jest you should use jest-codemods instead.

Usage

import test from 'jest-t-assert';

test('a test case', t => {
  t.true(2 < 10);
  t.truthy(1);
  t.not(2, '2');
  t.deepEqual([1, 2], [1, 2]);
  t.throws(() => {
    throw new Error('This function throws');
  });
  t.snapshot({ a: 1, b: 'ok' });
});

You can also import t directly if you only want to use the assertions without the test wrapper. This doesn't break existing tests that use Jest's callback mode, where you call done(), which is passed as the first argument to the test function.

import { t } from 'jest-t-assert';

test('only using the assertions from `t`', () => {
  t.true(2 < 10);
  t.truthy(1);
  t.not(2, '2');
  t.deepEqual([1, 2], [1, 2]);
  t.throws(() => {
    throw new Error('This function throws');
  });
  t.snapshot({ a: 1, b: 'ok' });
});

Asynchronous tests

Jest supports returning a promise from the test, so you can do the assertions in the .then calls.

import test from 'jest-t-assert';

test('promise resolves with the correct value', t => {
  return Promise.resolve('value').then(val => t.is(val, 'value'));
});

test('using async/await', async t => {
  const val = await Promise.resolve('value');
  t.is(val, 'value');
  // await directly
  t.is(await Promise.resolve('value'), 'value');
});

If you need to use the callback mode you have to use test.cb, which requires you to end the test manually by calling t.end(). It is highly recommended to use promises and async / await instead of the callback mode.

import test from 'jest-t-assert';

test.cb('a callback', t => {
  t.plan(1);
  setTimeout(() => {
    t.pass();
    t.end();
  }, 2000);
});

API

test([title], fn, [timeout])

Run a test. fn will receive t as the first argument, which provides the assertions. The timeout specifies how long (in milliseconds) to wait until the test is aborted.

test.cb([title], fn, [timeout])

Run a test in callback mode. fn will receive t, which provides the usual assertions and additionally t.end(), which must be called to manually end the test. .cb can be chained with any of modifiers listed below. The timeout specifies how long (in milliseconds) to wait until the test is aborted.

test.only([title], fn, [timeout])

Only run this test. All other tests in the test suite are skipped. The timeout specifies how long (in milliseconds) to wait until the test is aborted.

test.skip([title], fn, [timeout])

Skip the test. Useful to avoid a failing test without removing it or commenting it out.

test.after(fn)

Runs fn after all tests in the test suite have completed.

test.afterEach(fn)

Runs fn after each test in the test suite has completed.

test.before(fn)

Runs fn before any test in the test suite is run.

test.beforeEach(fn)

Runs fn before each test in the test suite is run.

t

The argument passed to the callback of test, which is also exported as t.

t.end()

End the test. This is only available when using test.cb.

t.plan(count)

Plan how many assertions are used in the test. The test fails if the number of assertions differs from count.

t.pass()

An assertion that always passes.

t.fail()

An assertion that always fails.

t.true(actual)

Assert that actual is true.

t.false(actual)

Assert that actual is false.

t.truthy(actual)

Assert that actual is truthy.

t.falsy(actual)

Assert that actual is falsy.

t.is(actual, expected)

Assert that actual is equal to expected (based on ===).

t.not(actual, expected)

Assert that actual is not equal to expected (based on ===). The inverse of t.is(actual, expected).

t.deepEqual(actual, expected)

Assert that actual is deeply equal to expected. This recursively checks the equality of all fields.

t.notDeepEqual(actual, expected)

Assert that actual is not deeply equal to expected. This recursively checks the equality of all fields. The inverse of t.deepEqual(actual, expected).

t.throws(fn, [error])

Assert that fn throws an error. If error is provided, the thrown error must match it.

t.notThrows(fn)

Assert that fn does not throw an error. The inverse of t.throws(fn).

t.regex(actual, regex)

Assert that actual matches regex.

t.notRegex(actual, regex)

Assert that actual does not match regex. The inverse of t.regex(actual, regex).

t.snapshot(actual)

Assert that actual matches the most recent snapshot.