@giancosta86/more-jest-io
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

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.

Package Sidebar

Install

npm i @giancosta86/more-jest-io

Weekly Downloads

0

Version

2.0.1

License

MIT

Unpacked Size

26.3 kB

Total Files

47

Last publish

Collaborators

  • giancosta86