From 93c44e1b0961fe825eb15f8c2ea37c4d5fd3dc08 Mon Sep 17 00:00:00 2001 From: Sourabh Desai <sourabhdesai@gmail.com> Date: Tue, 27 Jun 2023 07:15:51 +0000 Subject: [PATCH] small unit test for walk method --- .../core/src/tests/GenericFileSystem.test.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/core/src/tests/GenericFileSystem.test.ts b/packages/core/src/tests/GenericFileSystem.test.ts index d86d6c18e..4ae756332 100644 --- a/packages/core/src/tests/GenericFileSystem.test.ts +++ b/packages/core/src/tests/GenericFileSystem.test.ts @@ -3,6 +3,7 @@ import { getNodeFS, InMemoryFileSystem, exists, + walk, } from "../storage/FileSystem"; import os from "os"; import path from "path"; @@ -99,3 +100,34 @@ describe.each<FileSystemUnderTest>([ }); }); }); + +describe("Test walk for Node.js fs", () => { + const fs = getNodeFS(); + let tempDir: string; + + beforeAll(async () => { + tempDir = await nodeFS.mkdtemp(path.join(os.tmpdir(), "jest-")); + await fs.writeFile(`${tempDir}/test.txt`, "Hello, world!"); + await fs.mkdir(`${tempDir}/subDir`); + await fs.writeFile(`${tempDir}/subDir/test2.txt`, "Hello, again!"); + }); + + it("walks directory", async () => { + const expectedFiles = new Set([ + `${tempDir}/subDir/test2.txt`, + `${tempDir}/test.txt`, + ]); + + const actualFiles = new Set<string>(); + for await (let file of walk(fs, tempDir)) { + expect(file).toBeTruthy(); + actualFiles.add(file); + } + + expect(expectedFiles).toEqual(actualFiles); + }); + + afterAll(async () => { + await nodeFS.rm(tempDir, { recursive: true }); + }); +}); -- GitLab