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