From 31b05876bb59b46d8813d51c2a3f94d4d04aff73 Mon Sep 17 00:00:00 2001 From: Manos Konstantinidis Date: Mon, 11 Oct 2021 14:48:32 +0100 Subject: [PATCH] Write tests for file-exists --- src/utils/file-exists.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/file-exists.test.ts diff --git a/src/utils/file-exists.test.ts b/src/utils/file-exists.test.ts new file mode 100644 index 00000000..ee8361b4 --- /dev/null +++ b/src/utils/file-exists.test.ts @@ -0,0 +1,27 @@ +import { promises as fs } from 'fs'; + +import { fileExists } from './file-exists'; + +describe('file-exists.ts', () => { + const accessMock = jest.spyOn(fs, 'access'); + + beforeEach(() => { + accessMock.mockReset(); + }); + + it('should check if a file exists - true', async () => { + accessMock.mockResolvedValueOnce(); + + const result = await fileExists('./hello.txt'); + + expect(result).toBe(true); + }); + + it('should check if a file exists - false', async () => { + accessMock.mockRejectedValueOnce(undefined); + + const result = await fileExists('./file-does-not-exist.txt'); + + expect(result).toBe(false); + }); +});