Skip to content

Commit

Permalink
c Document how to use fusion-fs with jest
Browse files Browse the repository at this point in the history
  • Loading branch information
jwloka committed Apr 21, 2024
1 parent bcd7fef commit 29d68c5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/core/test/fusion-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ import type { IReadStream } from "memfs/lib/node/types/misc";
import type { IReadStreamOptions, IReaddirOptions } from "memfs/lib/node/types/options";
import { dirname } from "path";

/**
* Creates a new fs module that uses the memfs volume as a base.
* This can be very useful for testing purposes. The actual fs module is used as a fallback.
*
* For example in your `jest.setup.js` file you can use this function to replace the fs module as follows:
*
* import { createFs, resetFs } from "./test/fusion-fs";
*
* jest.mock("fs", () => {
* return createFs(jest.requireActual("fs"));
* });
*
* afterEach(() => {
* jest.clearAllMocks();
* resetFs();
* });
*
*
* @param actualFs
* @returns
*/

export const createFs = (actualFs: typeof fs): typeof fs => {
const memfs = createFsFromVolume(vol);

Expand Down

0 comments on commit 29d68c5

Please sign in to comment.