2

I know jest does some re-write magic before executing tests, I'm just trying to figure out how to mock fs.lstat in a helper file.

let's take an example module:

/* module.js */
import { lstat } from 'fs';

export default function () {
  return lstat();
}

and a minimal example test file that works:

/* module.test.js */
import module from './module';

// start
import * as fs from 'fs';
jest.mock('fs');
const mockedFs = fs as jest.Mocked<typeof fs>;
mockedFs.lstat.mockReturnValue(42);
// end

test('test', () => {
  expect(module()).toBe(42);
});

That works as is. If I take everything between the start and end comments and move it into another file, then import that, it doesn't work.

1 Answers1

2

Answering my own question.

Leaving just jest.mock('fs') in actual test file allows mock functions to run from other imports.