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

@giancosta86/more-jest-io

v2.0.1

Published

TypeScript I/O matchers and utilities for Jest

Downloads

14

Readme

more-jest-io

TypeScript I/O matchers and utilities for Jest

GitHub CI npm version MIT License

more-jest-io is a TypeScript library providing I/O-related matchers and utilities for Jest.

Installation

The NPM package is:

@giancosta86/more-jest-io

The public API entirely resides in the root package index, so one shouldn't reference specific modules from TypeScript code.

Global matchers

In order to be able to access the custom matchers provided by this library within any test file in your project, please follow these steps:

  1. Create a declaration file (for example, global.d.ts) in the root directory of your project, containing the following line:

    import "@giancosta86/more-jest-io";
  2. In the jest.config.js configuration file, add an entry to the setupFilesAfterEnv array field:

    module.exports = {
      setupFilesAfterEnv: ["@giancosta86/more-jest-io/dist/all"]
    };

Matchers

  • toBeTextFile(expectedTextContent[,{encoding: "utf-8"}]): an asynchronous matcher ensuring that the received file path is a text file with the expected text:

    //Reading with utf-8 encoding
    await expect(myTempFilePath).toBeTextFile("Alpha\nBeta\nGamma");
    
    //Reading with latin1 encoding
    await expect(myFrenchFilePath).toBeTextFile("Je préférérais une œvre d'art", {
      encoding: "latin1"
    });
  • toExist(): asynchronous matcher ensuring that a file system path exists. For example:

    await expect(myPath).toExist();
    
    await expect(inexistingPath).not.toExist();
  • toHaveStats(<predicate on Stats object>): asynchronous matcher ensuring that a file system path exists and satisfies the given predicate about its Stats.

    For example:

    await expect(myPath).toHaveStats(stats => stats);

Utilities

  • getSourceDirectory([callingScriptPath]): given a script path - which may reside at any level under the src or the dist directory of a project - returns the path of its src directory.

    It is especially useful in tests, to retrieve external files (such as test databases, test binary files, ...) that must be accessible while running tests from both TypeScript sources and compiled JS scripts - without copying files.

    If callingScriptPath is omitted, the path of the current script executed by Jest will be passed.

    If the source directory cannot be detected for a variety of reasons, the function throws a descriptive error.

    Example usage:

    describe("something", () => {
      it("should...", () => {
        const anyFile = join(getSourceDirectory(), "testData", "alpha.bin");
    
        //Access the file no matter where your test resides
      });
    });
  • useVolatileTempDirectory([options]): returns a temporary directory that:

    • is unique - as its name is based on a UUID

    • is managed by the test infrastructure - automatically deleted at the end of each test (the default) or at the end of the enclosing describe block.

    Consequently, it should be called just once - in a describe body - assigning its value to a constant that can be safely used within sub-block, especially test cases:

    describe("something", () => {
      const myTempDirectoryPath = useVolatileTempDirectory();
    
      it("should...", () => {
        //Here, myTempDirectoryPath is ready and empty
      });
    
      it("should...", () => {
        //Here, too, myTempDirectoryPath is ready and empty,
        //unless you pass {shared: true}
      });
    });

    If you pass {shared: true}, the temporary directory will have the same lifecycle as its enclosing describe block, thus enabling file sharing between tests and sub-blocks in general.