title: In Jest, mock a Node module that's in a user module, in a test block
body: <p>I have a user module called <code>files.js</code>. It uses the <a href="https://github.com/sindresorhus/globby" rel="nofollow noreferrer">globby</a> Node module, like so:</p>
  
  <pre><code>const globby = require('globby');
  
  module.exports = {
    /**
     * Get the paths for files in the current directory.
     * 
     * @returns {Promise&lt;string[]&gt;} The file paths.
     */
    async getFiles() {
      return await globby(__dirname);
    },
  };
  </code></pre>
  
  <p>I have a <code>files.test.js</code> test file, like so:</p>
  
  <pre><code>const globby = require('globby');
  const path = require('path');
  
  const files = require('./files');
  
  describe('files', () =&gt; {
    test('get files', async () =&gt; {
      const items = await files.getFiles();
  
      // The files that we expect are the ones in the current directory. Prepend
      // the current directory to each filename, so that they are absolute paths.
      const expectedFiles = ['files.js', 'files.test.js'];
      const expected = expectedFiles.map((file) =&gt; path.join(__dirname, file));
  
      expect(items).toEqual(expected);
    });
  
    test('get files (mocked)', async () =&gt; {
      // Try to mock the `globby` module.
      jest.mock('globby');
  
      globby.mockResolvedValue(['Test.js']);
  
      // Get the files, but expect the mocked value that we just set.
      const items = await files.getFiles();
  
      expect(items).toEqual(['Test.js']);
    });
  });
  </code></pre>
  
  <p>The first test passes fine, but the second test fails, because the resolved value for <code>globby</code> failed to mock properly. I've tried this with <code>jest.mock</code>, <code>jest.doMock</code>, etc. but I am unable to properly mock the value of <code>globby</code>, so that it returns <code>['Test.js']</code> in the call to <code>globby</code> in <code>getFiles</code>.</p>
  
  <p><strong>How can I properly mock the resolved value of <code>globby</code> so that it returns what I want in a single test block, from <code>getFiles</code>?</strong></p>
  
  <p>At the moment, I simply split the tests into two files, with one file containing the tests that require <code>globby</code> be unmocked, and the other file containing tests that require <code>globby</code> be mocked, but I am hoping for a more elegant solution that can do this all in the same file.</p>