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 './', 47 [ 48 // Skips this file 49 '.babelrc', 50 // Starts observing 51 'babel.config.js', 52 ], 53 { additionalWarning: ' foobar' } 54 ); 55 expect(fileNotifier.startObserving()).toBe('babel.config.js'); 56 57 // We mock out the callback firing and test that a warning was logged. 58 expect(Log.log).toBeCalledTimes(1); 59 expect(Log.log).toBeCalledWith(expect.stringContaining('babel.config.js')); 60 expect(Log.log).toBeCalledWith(expect.stringContaining('foobar')); 61}); 62