title: How to mock node 'createReadStream' and 'readline.createInterface' in jest tests
body: <p>I am encountering this issue when writing unit tests for code which involves 'createReadStream' and 'readline.createInterface'.</p>
  <p>Below is the code that I need to test:</p>
  <pre><code>private createReadStreamSafe(filePath: string): Promise&lt;fs.ReadStream&gt; {
          return new Promise((resolve, reject) =&gt; {
            const fileStream = fs.createReadStream(filePath)
            console.log('file Stream')
            fileStream
              .on('error', () =&gt; {
                reject('create read stream error')
              })
              .on('open', () =&gt; {
                resolve(fileStream)
              })
          })
        }
      
        async start() {
          const fileStream = await this.createReadStreamSafe(this.filePath)
      
          const rl = readline.createInterface({
            input: fileStream,
            output: process.stdout,
            terminal: false
          })
      
          for await (const line of rl) {
            ...
          }
        }
  </code></pre>
  <p>Below is my test,</p>
  <pre><code>it.only('should create an error if creating the read stream from the file path fails', () =&gt; {
      const mockedReadStream = new Readable()
      jest.spyOn(fs, 'createReadStream').mockReturnValue(mockedReadStream as any)
      const app = createApp('invalid/file/path')
  
      expect.assertions(1)
      try {
        app.start()
        mockedReadStream.emit('error', 'Invalid file path')
      } catch (error) {
        expect(getErrorMessage(error)).toBe('Invalid file path')
      }
    })
  </code></pre>
  <p>But, I got this:</p>
  <pre><code>node:internal/process/promises:246
            triggerUncaughtException(err, true /* fromPromise */);
            ^
  
  [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason &quot;undefined&quot;.] {
    code: 'ERR_UNHANDLED_REJECTION'
  }
  node:internal/process/promises:246
            triggerUncaughtException(err, true /* fromPromise */);
  </code></pre>