@giancosta86/more-jest-io
v2.0.1
Published
TypeScript I/O matchers and utilities for Jest
Downloads
5
Maintainers
Readme
more-jest-io
TypeScript I/O matchers and utilities for Jest
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:
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";
In the
jest.config.js
configuration file, add an entry to thesetupFilesAfterEnv
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 itsStats
.For example:
await expect(myPath).toHaveStats(stats => stats);
Utilities
getSourceDirectory([callingScriptPath])
: given a script path - which may reside at any level under thesrc
or thedist
directory of a project - returns the path of itssrc
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 enclosingdescribe
block, thus enabling file sharing between tests and sub-blocks in general.