Skip to content

Commit

Permalink
Testing Mock FileSystem
Browse files Browse the repository at this point in the history
Signed-off-by: vr-varad <[email protected]>
  • Loading branch information
vr-varad committed Nov 16, 2024
1 parent 16305ac commit 69d7f7d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/testing/__test__/fileSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'node:fs/promises';
import path from 'node:path';

const testFilePath = path.join(__dirname, 'testFile.txt');
const testDirPath = path.join(__dirname, 'testDir');

describe('FileSystem', () => {
beforeAll(async () => {
Expand All @@ -19,9 +20,9 @@ describe('FileSystem', () => {
expect(result.size).toBe(27);
});

test('should have a copyFile method', () => {
test('should have a copyFile method', async () => {
const copyPath = path.join(__dirname, 'copyFile.txt');
yasumu.fs.copyFile(testFilePath, copyPath);
await yasumu.fs.copyFile(testFilePath, copyPath);
expect(fs.readFile(copyPath, { encoding: 'utf-8' })).resolves.toBe('Initial content for testing');
yasumu.fs.remove(copyPath);
});
Expand All @@ -30,12 +31,24 @@ describe('FileSystem', () => {
expect(yasumu.fs.exists(testFilePath)).resolves.toBe(true);
});

test('should have a mkdir method', () => {
expect(yasumu.fs.mkdir).toBeDefined();
test('should have a mkdir method', async () => {
await yasumu.fs.mkdir(testDirPath);
const exists = await yasumu.fs.exists(testDirPath);
expect(exists).toBe(true);

const stats = await yasumu.fs.lstat(testDirPath);
expect(stats.isDirectory).toBe(true);
});

test('should have a readDir method', () => {
expect(yasumu.fs.readDir).toBeDefined();
test('should have a readDir method', async () => {
const file1 = path.join(testDirPath, 'file1.txt');
await fs.writeFile(file1, 'File 1 content');
const entries = await yasumu.fs.readDir(testDirPath);
const entryNames = entries.map((entry) => entry.name);

expect(entries).toBeDefined();
expect(Array.isArray(entries)).toBe(true);
expect(entryNames).toContain('file1.txt');
});

test('should have a readFile method', async () => {
Expand All @@ -44,7 +57,7 @@ describe('FileSystem', () => {
});

test('should have a remove method', async () => {
yasumu.fs.remove(testFilePath);
await yasumu.fs.remove(testFilePath);
const result = await yasumu.fs.exists(testFilePath);
expect(result).toBe(false);
});
Expand Down
1 change: 1 addition & 0 deletions packages/testing/__test__/testDir/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File 1 content

0 comments on commit 69d7f7d

Please sign in to comment.