Home > OS >  How to mock 'readline.createInterface' in jest tests
How to mock 'readline.createInterface' in jest tests

Time:12-22

I need to test 'readline.createInterface'.

Below is the code that I need to test:

private createReadStreamSafe(filePath: string): Promise<fs.ReadStream> {
        return new Promise((resolve, reject) => {
          const fileStream = fs.createReadStream(filePath)
          console.log('file Stream')
          fileStream
            .on('error', () => {
              reject('create read stream error')
            })
            .on('open', () => {
              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) {
          ...
        }
      }

I tried following code:

it('should work', async () => {
    const mockedReadStream = new Readable()
    jest.spyOn(fs, 'createReadStream').mockReturnValue(mockedReadStream as any)
    jest.spyOn(readline, 'createInterface').mockImplementation(() => {
      const lines = ['text', 'text2', 'text3']

      return {
        [Symbol.asyncIterator]() {
          return {
            i: 0,
            next: () => {
              if (this.i < 3) {
                return Promise.resolve({ value: lines[this.i  ], done: false })
              }

              return Promise.resolve({ done: true })
            }
          }
        }
      } as any
    })

    const app = new App('myFile.txt')

    let promise = app.start()
    mockedReadStream.emit('open')
    await expect(promise).resolves.toBe(undefined)
  })

But following code is never reached

for await (const line of rl) {
...
        }

Is there a way to mock readline.createInterface and then it works with the for await (const line of rl)?

CodePudding user response:

The issue is: the async iterable object is not triggered during the test.

Solution, we can just use an array in the mock, like this:

jest.spyOn(readline, 'createInterface').mockImplementationOnce(() => {
      return ['text1', 'text2'] as any
    })

Since for await (const item of iterable) works for async iterable objects as well sync iterables. With sync iterables, they will be executed automatically.

  • Related