1import { fs, vol } from 'memfs'; 2 3import { asMock } from '../../__tests__/asMock'; 4import * as Log from '../../log'; 5import { FileNotifier } from '../FileNotifier'; 6 7jest.mock('../../log'); 8 9const originalCwd = process.cwd(); 10 11beforeEach(() => { 12 vol.reset(); 13}); 14 15beforeAll(() => { 16 process.chdir('/'); 17 // @ts-expect-error 18 fs.watchFile = jest.fn(fs.watchFile); 19}); 20 21afterAll(() => { 22 process.chdir(originalCwd); 23}); 24 25it('returns null when no files can be found', () => { 26 vol.fromJSON({}, '/'); 27 const fileNotifier = new FileNotifier('./', ['babel.config.js']); 28 expect(fileNotifier.startObserving()).toBe(null); 29}); 30 31it('observes the first existing file', () => { 32 asMock(fs.watchFile) 33 // @ts-expect-error 34 .mockImplementationOnce((_, callback) => { 35 // @ts-expect-error: polymorphism 36 callback({}, { size: 1 }); 37 }); 38 39 vol.fromJSON( 40 { 41 'babel.config.js': '', 42 }, 43 '/' 44 ); 45 const fileNotifier = new FileNotifier('./', [ 46 // Skips this file 47 '.babelrc', 48 // Starts observing 49 'babel.config.js', 50 ]); 51 expect(fileNotifier.startObserving()).toBe('babel.config.js'); 52 53 // We mock out the callback firing and test that a warning was logged. 54 expect(Log.log).toBeCalledTimes(1); 55 expect(Log.log).toBeCalledWith(expect.stringContaining('babel.config.js')); 56}); 57