title: Jest - mock function in 'child_process' package
body: <p>I'm writing unit tests and which to mock the 'exec' method in package 'child_process'.</p>
  
  <p><strong>__mocks__/child_process.js</strong></p>
  
  <pre><code>const child_process = jest.genMockFromModule('child_process');
  child_process.exec = jest.fn()
  
  module.exports = child_process;
  </code></pre>
  
  <p><strong>This is the test file:</strong></p>
  
  <pre><code>const fs = require('fs-extra'),
        child_process = require('child_process'),
        runCassandraMigration = require('../../lib/runCassandraMigration.js')
  
  const defaultArguments = () =&gt; {
    return {
      migration_script_path: './home',
      logger: {
        error: function () {}
      }
    };
  }
  
  jest.mock("fs-extra")
  jest.mock("child_process")
  
  describe('Running cassandra migration tests', function () {
    describe('successful flow', function () {
      it('Should pass without any errors ', async function () {
          let args = defaultArguments();
          let loggerSpy = jest.spyOn(args.logger, 'error')
  
          fs.remove.mockImplementation(() =&gt; {Promise.resolve()})
          child_process.exec.mockImplementation(() =&gt; {Promise.resolve()})
  
          await runCassandraMigration(args.migration_script_path, args.logger)
      });
    });
  </code></pre>
  
  <p>When I run the test I get the following error:</p>
  
  <pre><code>child_process.exec.mockImplementation is not a function
  </code></pre>
  
  <p><strong>The module I test</strong></p>
  
  <pre><code>const fs = require('fs-extra')
  const promisify = require('util').promisify
  const execAsync = promisify(require('child_process').exec)
  
  module.exports = async (migration_script_path, logger) =&gt; {
    try {
      console.log()
      const {stdout, stderr} = await execAsync(`cassandra-migration ${migration_script_path}`)
      logger.info({stdout: stdout, stderr: stderr}, 'Finished runing cassandra migration')
      await fs.remove(migration_script_path)
    } catch (e) {
      logger.error(e, 'Failed to run cassandra migration')
      throw Error()
    }
  }
  </code></pre>
  
  <p>Please advise.</p>