UNPKG

3.62 kBTypeScriptView Raw
1/**
2 * Options for a test sandbox
3 */
4export interface TestSandboxOptions {
5 /**
6 * The `subdir` controls if/how the sandbox creates a subdirectory under the
7 * root path. It has one of the following values:
8 *
9 * - `true`: Creates a unique subdirectory. This will be the default behavior.
10 * - `false`: Uses the root path as the target directory without creating a
11 * subdirectory.
12 * - a string such as `sub-dir-1`: creates a subdirectory with the given value.
13 */
14 subdir: boolean | string;
15}
16/**
17 * TestSandbox class provides a convenient way to get a reference to a
18 * sandbox folder in which you can perform operations for testing purposes.
19 */
20export declare class TestSandbox {
21 private _path?;
22 get path(): string;
23 /**
24 * Will create a directory if it doesn't already exist. If it exists, you
25 * still get an instance of the TestSandbox.
26 *
27 * @example
28 * ```ts
29 * // Create a sandbox as a unique temporary subdirectory under the rootPath
30 * const sandbox = new TestSandbox(rootPath);
31 * const sandbox = new TestSandbox(rootPath, {subdir: true});
32 *
33 * // Create a sandbox in the root path directly
34 * // This is same as the old behavior
35 * const sandbox = new TestSandbox(rootPath, {subdir: false});
36 *
37 * // Create a sandbox in the `test1` subdirectory of the root path
38 * const sandbox = new TestSandbox(rootPath, {subdir: 'test1'});
39 * ```
40 *
41 * @param rootPath - Root path of the TestSandbox. If relative it will be
42 * resolved against the current directory.
43 * @param options - Options to control if/how the sandbox creates a
44 * subdirectory for the sandbox. If not provided, the sandbox
45 * will automatically creates a unique temporary subdirectory. This allows
46 * sandboxes with the same root path can be used in parallel during testing.
47 */
48 constructor(rootPath: string, options?: TestSandboxOptions);
49 /**
50 * Resets the TestSandbox. (Remove all files in it).
51 */
52 reset(): Promise<void>;
53 /**
54 * Deletes the TestSandbox.
55 */
56 delete(): Promise<void>;
57 /**
58 * Makes a directory in the TestSandbox
59 *
60 * @param dir - Name of directory to create (relative to TestSandbox path)
61 */
62 mkdir(dir: string): Promise<void>;
63 /**
64 * Copies a file from src to the TestSandbox. If copying a `.js` file which
65 * has an accompanying `.js.map` file in the src file location, the dest file
66 * will have its sourceMappingURL updated to point to the original file as
67 * an absolute path so you don't need to copy the map file.
68 *
69 * @param src - Absolute path of file to be copied to the TestSandbox
70 * @param dest - Optional. Destination filename of the copy operation
71 * (relative to TestSandbox). Original filename used if not specified.
72 * @param transform - Optional. A function to transform the file content.
73 */
74 copyFile(src: string, dest?: string, transform?: (content: string) => string): Promise<void>;
75 /**
76 * Creates a new file and writes the given data serialized as JSON.
77 *
78 * @param dest - Destination filename, optionally including a relative path.
79 * @param data - The data to write.
80 */
81 writeJsonFile(dest: string, data: unknown): Promise<void>;
82 /**
83 * Creates a new file and writes the given data as a UTF-8-encoded text.
84 *
85 * @param dest - Destination filename, optionally including a relative path.
86 * @param data - The text to write.
87 */
88 writeTextFile(dest: string, data: string): Promise<void>;
89}