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-to-log

v1.1.0

Published

Test log messages with jest

Downloads

14

Readme

jest-to-log

What

Test log messages with jest

Why?

Grown out of the need to test console.log/error messages or writes to process.stdout/stderr in my cli-facing apps. Note: this is not a full-blown cli-testing tool, but a check for important log messages from your app that's tested with jest.

Usage

require("jest-to-log");
//or
import "jest-to-log";

Example

//in jest test file
require("jest-to-log");

describe("toLog", () => {

  it("should capture log messages", () => {

    function testFn() {

      console.info("Jello", 1);
      console.log("Jello", 2);
      console.debug("Jello", 3);
    }
    
    const expectedString =
      "Jello 1" + "\n" + "Jello 2" + "\n" + "Jello 3" + "\n";

    expect(testFn).toLog(expectedString);
  });
});

Async Example

When an async function is tested, the expect assertions must be awaited.

//in jest test file (esm)
import("jest-to-log");

describe("process.stdout.write", () => {

  it("should capture process.stdout.write messages", async () => {

    async function asyncTestFn() {

      await new Promise((resolve) => {

        setTimeout(() => {
          process.stdout.write(`Jello 123\n`);
          process.stdout.write(`Jello 456\n`);

          resolve();

        }, 500);
      });
    }

    const expectedString = "Jello 123" + "\n" + "Jello 456" + "\n";

    await expect(asyncTestFn).toLog(expectedString);
  });
});

Caveat

  • When using with typescript (i.e., ts-jest and @types/jest), it's advised to explicitly import the expect from @jest/globals to avoid type conflicts:
//test.ts

import { expect } from "@jest/globals";

// ...your test
  • Do NOT set the injectGlobals jest option to false, or else a ReferenceError will be thrown. This is because the matchers are extended onto the expect object without importing it from @jest/globals.

Matchers

| Matcher Name | Description | | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | toLog | Checks if a function logs a message via console.log(), console.info() or console.debug(). | | toLogStdout | Checks if a function explicitly writes to process.stdout. Cannot be used to capture console.log/info/debug calls due to the custom console implementation of jest. | | toLogStderr | Checks if a function explicitly write to process.stderr. Cannot be used to capture console.error/warn calls due to the custom console implementation of jest. | | toLogErrorOrWarn | Checks if a function logs an error or warning via console.error() or console.warn(), respectively. |

Installation

$ npm install jest-to-log

Requirements

  • Latest versions of Jest recommended

Dependencies

  • 0 javascript/nodejs dependency!

Test

$ npm test