title: Mock fs function with jest
body: <p>First of all, I'm new to <code>es6</code> and <code>jest</code>.</p>
  
  <p>I have a <code>Logger</code> class for instantiate <code>winston</code> and I would like to test it.</p>
  
  <p>Here my code :</p>
  
  <pre><code>const winston = require('winston');
  const fs = require('fs');
  const path = require('path');
  const config = require('../config.json');
  
  class Logger {
    constructor() {
      Logger.createLogDir(Logger.logDir);
      this.logger = winston.createLogger({
        level: 'info',
        format: winston.format.json(),
        transports: [
          new (winston.transports.Console)({
            format: winston.format.combine(
              winston.format.colorize({ all: true }),
              winston.format.simple(),
            ),
          }),
          new (winston.transports.File)({
            filename: path.join(Logger.logDir, '/error.log'),
            level: 'error',
          }),
          new (winston.transports.File)({
            filename: path.join(Logger.logDir, '/info.log'),
            level: 'info',
          }),
          new (winston.transports.File)({
            filename: path.join(Logger.logDir, '/combined.log'),
          }),
        ],
      });
    }
  
    static get logDir() {
      return (config.logDir == null) ? 'log' : config.logDir;
    }
  
    static createLogDir(logDir) {
      if (!fs.existsSync(logDir)) {
        // Create the directory if it does not exist
        fs.mkdirSync(logDir);
      }
    }
  }
  
  exports.logger = new Logger().logger;
  export default new Logger();
  </code></pre>
  
  <p>I would like to test my function <code>createLogDir()</code>.
  I my head, I think it's a good idea to test the state of fs.existsSync.
  If <code>fs.existsSync</code> return <code>false</code>, <code>fs.mkdirSync</code> must  be called.
  So I try to write some <code>jest</code> test :</p>
  
  <pre><code>describe('logDir configuration', () =&gt; {
    test('default path must be used', () =&gt; {
      const logger = require('./logger');
      jest.mock('fs');
      fs.existsSync = jest.fn();
      fs.existsSync.mockReturnValue(false);
      const mkdirSync = jest.spyOn(logger, 'fs.mkdirSync');
      expect(mkdirSync).toHaveBeenCalled();
    });
  });
  </code></pre>
  
  <p>However, I've got an error :</p>
  
  <pre><code>  ● logDir configuration › default path must be used
  
      Cannot spy the fs.mkdirSync property because it is not a function; undefined given instead
  
        18 |     fs.existsSync = jest.fn();
        19 |     fs.existsSync.mockReturnValue(true);
      &gt; 20 |     const mkdirSync = jest.spyOn(logger, 'fs.mkdirSync');
        21 |     expect(mkdirSync).toHaveBeenCalled();
        22 |   });
        23 | });
  
        at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:590:15)
        at Object.test (src/logger.test.js:20:28)
  </code></pre>
  
  <p>Can you help me to debug and test my function please ?</p>
  
  <p>Regards.</p>