/** * A context that allows for more advanced file system operations when writing mock directory content. * * @public */ export interface MockDirectoryContentCallbackContext { /** Absolute path to the location of this piece of content on the filesystem */ path: string; /** Creates a symbolic link at the current location */ symlink(target: string): void; } /** * A callback that allows for more advanced file system operations when writing mock directory content. * * @public */ export type MockDirectoryContentCallback = (ctx: MockDirectoryContentCallbackContext) => void; /** * The content of a mock directory represented by a nested object structure. * * @remarks * * When used as input, the keys may contain forward slashes to indicate nested directories. * Then returned as output, each directory will always be represented as a separate object. * * @example * ```ts * { * 'test.txt': 'content', * 'sub-dir': { * 'file.txt': 'content', * 'nested-dir/file.txt': 'content', * }, * 'empty-dir': {}, * 'binary-file': Buffer.from([0, 1, 2]), * } * ``` * * @public */ export type MockDirectoryContent = { [name in string]: MockDirectoryContent | string | Buffer | MockDirectoryContentCallback; }; /** * Options for {@link MockDirectory.content}. * * @public */ export interface MockDirectoryContentOptions { /** * The path to read content from. Defaults to the root of the mock directory. * * An absolute path can also be provided, as long as it is a child path of the mock directory. */ path?: string; /** * Whether or not to return files as text rather than buffers. * * Defaults to checking the file extension against a list of known text extensions. */ shouldReadAsText?: boolean | ((path: string, buffer: Buffer) => boolean); } /** * A utility for creating a mock directory that is automatically cleaned up. * * @public */ export interface MockDirectory { /** * The path to the root of the mock directory */ readonly path: string; /** * Resolves a path relative to the root of the mock directory. */ resolve(...paths: string[]): string; /** * Sets the content of the mock directory. This will remove any existing content. * * @example * ```ts * mockDir.setContent({ * 'test.txt': 'content', * 'sub-dir': { * 'file.txt': 'content', * 'nested-dir/file.txt': 'content', * }, * 'empty-dir': {}, * 'binary-file': Buffer.from([0, 1, 2]), * }); * ``` */ setContent(root: MockDirectoryContent): void; /** * Adds content of the mock directory. This will overwrite existing files. * * @example * ```ts * mockDir.addContent({ * 'test.txt': 'content', * 'sub-dir': { * 'file.txt': 'content', * 'nested-dir/file.txt': 'content', * }, * 'empty-dir': {}, * 'binary-file': Buffer.from([0, 1, 2]), * }); * ``` */ addContent(root: MockDirectoryContent): void; /** * Reads the content of the mock directory. * * @remarks * * Text files will be returned as strings, while binary files will be returned as buffers. * By default the file extension is used to determine whether a file should be read as text. * * @example * ```ts * expect(mockDir.content()).toEqual({ * 'test.txt': 'content', * 'sub-dir': { * 'file.txt': 'content', * 'nested-dir': { * 'file.txt': 'content', * }, * }, * 'empty-dir': {}, * 'binary-file': Buffer.from([0, 1, 2]), * }); * ``` */ content(options?: MockDirectoryContentOptions): MockDirectoryContent | undefined; /** * Clears the content of the mock directory, ensuring that the directory itself exists. */ clear(): void; /** * Removes the mock directory and all its contents. */ remove(): void; } /** * Options for {@link createMockDirectory}. * * @public */ export interface CreateMockDirectoryOptions { /** * In addition to creating a temporary directory, also mock `os.tmpdir()` to * return the mock directory path until the end of the test suite. * * When this option is provided the `createMockDirectory` call must happen in * a scope where calling `afterAll` from Jest is allowed * * @returns */ mockOsTmpDir?: boolean; /** * Initializes the directory with the given content, see {@link MockDirectory.setContent}. */ content?: MockDirectoryContent; } /** * Creates a new temporary mock directory that will be removed after the tests have completed. * * @public * @remarks * * This method is intended to be called outside of any test, either at top-level or * within a `describe` block. It will call `afterAll` to make sure that the mock directory * is removed after the tests have run. * * @example * ```ts * describe('MySubject', () => { * const mockDir = createMockDirectory(); * * beforeEach(mockDir.clear); * * it('should work', () => { * // ... use mockDir * }) * }) * ``` */ export declare function createMockDirectory(options?: CreateMockDirectoryOptions): MockDirectory;