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

mocha-ui-jest

v0.4.0

Published

A test interface for mocha that mimics jest's API

Downloads

64

Readme

mocha-ui-jest

This is an attempt to brings as much of the Jest API as possible to Mocha.

Why not just use Jest?

Jest is great, I use it extensively for unit testing React app code. However, when it comes to running integration tests that take image snapshots for visual regression testing, it's not so good. The Jest API doesn't provide a way to get information about individual test cases starting or stopping (see jest#4471). The unit of work that jest reports on is a test file, not a single test (see jest#6616). Jest also does all kinds of strange things with stdout (see jest#6718, jest#5918). Also, the way Jest handles the --bail option makes it hard to pinpoint where an error occurred (see jest#6527). Jest is also moderately difficult to set up with Puppeteer. These issues combine in making it very difficult to use Jest as an integration test runner for visual regression testing.

Mocha is vastly simpler than Jest, and doesn't have many of these issues or restrictions, but it lacks out of the box snapshot support, and I like Jest's syntax better. So I've added them here, while mimicing Jest's API as much as possible to make migrating back to Mocha easier.

Usage

yarn add --dev mocha-ui-jest
mocha --require mocha-ui-jest --ui jest <pathToYourTestFile.test.js>

Then you can write test files that work like your Jest tests:

describe('some test', () => {
  test('with snapshots', () => {
    expect([{ that: 'this' }]).toMatchSnapshot();
  });
]);

If you want to regenerate your snapshots, use the env var SNAPSHOT_UPDATE=true

SNAPSHOT_UPDATE=true mocha --require mocha-ui-jest --ui jest <pathToYourTestFile.test.js>

Reporter

This packages also bundles a reporter that extends the default 'spec' mocha reporter with some extra info about snapshot passes/adds/updates/failures.

To use it:

mocha --require mocha-ui-jest --ui jest --reporter=mocha-ui-jest/reporter <pathToYourTestFile.test.js>

You'll get extra output of end of the test run like the following if there are snapshots.

  Snapshots: 12 passed, 2 added, 1 updated, 3 failed

Getting more information about snapshot state

If all you want is a summary at the end, there are also included accumulator classes SnapshotAccumulator that will listen for the events and accumlate a state that you can read by calling the getState() method. You can stop the accumulator listenting for future events by calling its destroy() method.

const { SnapshotAccumulator } = require('mocha-ui-jest');

describe('using the snapshot accumulator', () => {
  let accum;
  beforeAll(() => {
    const accum = new SnapshotAccumulator();
  });
  test('does something with snapshots', () => {
    expect(true).toMatchSnapshot();
  });
  afterAll(() => {
    const { passCount, failCount, addCount, updateCount, failures } = accum.getState();
    accum.destroy();
    // do something with the data.
    console.log(`${passCount}, ${failCount}, ${updateCount}, ${addCount}`);
    // failures contains a list of objects that look like this, one for each snapshot that failed.
    // [{ 
    //   title: 'does something with snapshots',
    //   fullTitle: 'using the snapshot accumulator does something with snapshots',
    //   file: 'relative/path/to/test.file',
    //   snapshotFile: 'relative/path/to/shapshot.file',
    // }];
  });
});

What's missing

Missing support for the following jest features:

  • All of jest's mocking features
  • jest.setTimeout (you can use mocha's this.timeout(number) instead)
  • expect().toMatchInlineSnapshot();
  • expect().toThrowErrorMatchingSnapshot(),
  • expect().toThrowErrorMatchingInlineSnapshot(),

If someone wants to lend a hand getting those implemented, I'd be thankful!

Changelog:

0.4.0:

  • Removes image snapshot support. It didn't work well in the real world and was complicated.
  • Get mocha test context from mocha-test-context, which is much simpler than the previous way we got test context

License: MIT