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

babel-jest-ts-assertions

v0.1.1

Published

enable babel-jest-assertions support typescript

Downloads

4

Readme

🃏⁉️

Adds expect.assertions(n) and expect.hasAssertions to all tests automatically

Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples

Problem

Ever wondered if your tests are actually running their assertions, especially in asynchronous tests? Jest has two features built in to help with this: expect.assertions(number) and expect.hasAssertions(). These can be useful when doing something like:

it('resolves to one', () => {
  Promise.reject(1).then(value => expect(value).toBe(1));
});

The issue here is the catch case is not dealt with in this test, which is fine as we are testing the happy path, but this test will currently pass even though the Promise rejects and the assertion is never ran.

Solution

One solution is to manually adjust the above test to include expect.assertions(number) and expect.hasAssertions() this is quite verbose and prone to human error.

An alternative is a babel plugin to automate adding these additional properties, and this is such plugin 😉

Installation

With npm:

npm install --save-dev babel-jest-ts-assertions

With yarn:

yarn add -D babel-jest-ts-assertions

Setup

.babelrc

{
  "plugins": ["babel-jest-assertions"]
}

CLI

babel --plugins babel-jest-assertions script.js

Node

require('@babel/core').transform('code', {
  plugins: ['babel-jest-assertions'],
})

Usage

Simply write your tests as you would normally and this plugin will add the verification of assertions in the background.

One assertion

it('resolves to one', () => {
  Promise.reject(1).then(value => expect(value).toBe(1));
});

↓ ↓ ↓ ↓ ↓ ↓

it('resolves to one', () => {
  expect.hasAssertions();
  expect.assertions(1);
  Promise.reject(1).then(value => expect(value).toBe(1));
});

Note: this test will now fail 🎉

Multiple assertions

it('counts multiple assertions too', () => {
  expect(1 + 0).toBe(1);
  expect(0 + 1).toBe(1);
});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too', () => {
  expect.hasAssertions();
  expect.assertions(2);
  expect(1 + 0).toBe(1);
  expect(0 + 1).toBe(1);
});

Asynchronous assertions

it('counts multiple assertions too', async () => {
  const res = await fetch('www.example.com');
  expect(res.json).toBeTruthy();
  const json = await res.json();
  expect(json).toEqual({ whatever: 'trevor' });
});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too', async () => {
  expect.hasAssertions();
  expect.assertions(2);
  const res = await fetch('www.example.com');
  expect(res.json).toBeTruthy();
  const json = await res.json();
  expect(json).toEqual({ whatever: 'trevor' });
});

beforeEach and afterEach blocks

If you have expectations inside either of beforeEach or afterEach blocks for your test then these expects will be included in the count - even if you have nested describe blocks each with their own beforeEach/afterEach the count will accumulate.

beforeEach(() => {
  expect(true).toBe(true);
});

afterEach(() => {
  expect(true).toBe(true);
});

describe('.add', () => {
  beforeEach(() => {
    expect(true).toBe(true);
  });
  afterEach(() => {
    expect(true).toBe(true);
  });
  it('returns 1 when given 0 and 1', () => {
    expect(add(1, 0)).toEqual(1);
  });

  describe('.add2', () => {
    beforeEach(() => {
      expect(true).toBe(true);
    });
    afterEach(() => {
      expect(true).toBe(true);
    });
    it('returns 1 when given 0 and 1', () => {
      expect(add2(1, 0)).toEqual(1);
    });
  });
});

      ↓ ↓ ↓ ↓ ↓ ↓

beforeEach(() => {
  expect(true).toBe(true);
});

afterEach(() => {
  expect(true).toBe(true);
});

describe('.add', () => {
  beforeEach(() => {
    expect(true).toBe(true);
  });
  afterEach(() => {
    expect(true).toBe(true);
  });
  it('returns 1 when given 0 and 1', () => {
    expect.assertions(5);
    expect.hasAssertions();

    expect(add2(1, 0)).toEqual(1);
  });

  describe('.add2', () => {
    beforeEach(() => {
      expect(true).toBe(true);
    });
    afterEach(() => {
      expect(true).toBe(true);
    });
    it('returns 1 when given 0 and 1', () => {
      expect.assertions(7);
      expect.hasAssertions();

      expect(add2(1, 0)).toEqual(1);
    });
  });
});

Comments are ignored

it('ignores commented-out assertions', async () => {
  const res = await fetch('www.example.com');
  // expect(res.json).toBeTruthy();
  const json = await res.json();
  /*
    expect(json).toEqual({ whatever: 'trevor1' });
  */
  expect(json).toEqual({ whatever: 'trevor' });
  /* expect(json).toEqual({ whatever: 'trevor2' }); */
});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too', async () => {
  expect.hasAssertions();
  expect.assertions(1);
  const res = await fetch('www.example.com');
  // expect(res.json).toBeTruthy();
  const json = await res.json();
  /*
    expect(json).toEqual({ whatever: 'trevor1' });
  */
  expect(json).toEqual({ whatever: 'trevor' });
  /* expect(json).toEqual({ whatever: 'trevor2' }); */
});

Override

If you add either expect.assertions(number) or expect.hasAssertions() then your defaults will be favoured and the plugin will skip the test.

it('will leave test as override supplied', () => {
  expect.hasAssertions();
  expect.assertions(1);

  if (true) {
    expect(true).toBe(true);
  }

  if (false) {
    expect(false).toBe(false);
  }
});

↓ ↓ ↓ ↓ ↓ ↓

it('will leave test as override supplied', () => {
  expect.hasAssertions();
  expect.assertions(1);

  if (true) {
    expect(true).toBe(true);
  }

  if (false) {
    expect(false).toBe(false);
  }
});

Contributors

| Matt Phillips💻 📖 🚇 ⚠️ | Ramesh Nair💻 📖 💡 ⚠️ | Huy Nguyen🐛 | Simon Boudrias🐛 | Giuseppe🤔 | | :---: | :---: | :---: | :---: | :---: |

LICENSE

MIT