github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@kwsites/file-exists/dist/test/exists.spec.js (about) 1 "use strict"; 2 Object.defineProperty(exports, "__esModule", { value: true }); 3 jest.mock('fs'); 4 //@ts-ignore 5 const fs_1 = require("fs"); 6 const src_1 = require("../src"); 7 describe(`exists`, () => { 8 let statSync; 9 let statSyncMock; 10 let path; 11 beforeEach(() => { 12 path = `./path/${Math.random()}`; 13 fs_1.addStatSyncMock(statSyncMock = jest.fn(() => statSync())); 14 }); 15 afterEach(() => { 16 fs_1.assertMocksUsed(); 17 statSync = statSyncMock = undefined; 18 }); 19 describe('known errors', () => { 20 beforeEach(() => givenStatSyncThrows({ code: 'ENOENT' })); 21 it('with type', () => { 22 expect(src_1.exists(path, src_1.READABLE)).toBe(false); 23 }); 24 it('with type omitted', () => { 25 expect(src_1.exists(path)).toBe(false); 26 }); 27 }); 28 describe('unknown errors', () => { 29 let err; 30 beforeEach(() => err = givenStatSyncThrows(new Error('something'))); 31 it('with type', () => { 32 expect(() => src_1.exists(path, src_1.READABLE)).toThrow(err); 33 }); 34 it('with type omitted', () => { 35 expect(() => src_1.exists(path)).toThrow(err); 36 }); 37 }); 38 describe('path is a file', () => { 39 beforeEach(() => givenStatSyncIsA('file')); 40 existsReturns(true, false, true); 41 }); 42 describe('path is a folder', () => { 43 beforeEach(() => givenStatSyncIsA('folder')); 44 existsReturns(false, true, true); 45 }); 46 describe('path is unknown', () => { 47 beforeEach(() => givenStatSyncIsA('unknown')); 48 existsReturns(false, false, false); 49 }); 50 function existsReturns(file, folder, readable) { 51 it('when searching for a file', () => { 52 expect(src_1.exists(path, src_1.FILE)).toBe(file); 53 }); 54 it('when searching for a folder', () => { 55 expect(src_1.exists(path, src_1.FOLDER)).toBe(folder); 56 }); 57 it('when searching for either', () => { 58 expect(src_1.exists(path, src_1.READABLE)).toBe(readable); 59 }); 60 it('when searching without a type', () => { 61 expect(src_1.exists(path)).toBe(readable); 62 }); 63 } 64 function givenStatSyncThrows(err) { 65 statSync = () => { throw err; }; 66 return err; 67 } 68 function givenStatSyncIsA(type) { 69 const mockStat = { 70 isFile() { return type === 'file'; }, 71 isDirectory() { return type === 'folder'; }, 72 }; 73 statSync = () => mockStat; 74 return mockStat; 75 } 76 }); 77 //# sourceMappingURL=exists.spec.js.map