简体   繁体   English

NestJS,TypeScript,Jest -> TypeError:无法读取未定义的属性“管道”

[英]NestJS, TypeScript, Jest -> TypeError: Cannot read property 'pipe' of undefined

The case is to write a unit test for uploadFile which should upload file to Google Storage bucket using Jest and to mock createReadStream function on the File object.案例是为uploadFile编写一个单元测试,它应该使用 Jest 将文件上传到 Google Storage 存储桶,并在文件 object 上模拟createReadStream function。

my-service.ts : my-service.ts

async uploadFile(file: FileUpload): Promise<{
url: string
path: string}> {
 try {
  file.createReadStream().pipe(
   bucket
    .createWriteStream({
     ...some options
    })
    .on('error', (err) => {
     reject(err)})
    .on('finish', async () => {
     resolve({
      url: 'file-url',
      path: 'file-path'
     })
    })
   
 }
}

my-service.spec.ts : my-service.spec.ts

  describe('#uploadFile', () => {
    it('uploads file', async () => {
      const bucketMock = new Bucket('the-bucket-mock')
      const bucketFileMock = new File(bucketMock, 'the-file')

      const fileUploadMock = {
        filename: 'the-file',
        mimetype: 'mimetype',
        encoding: 'encoding',
        createReadStream: jest.fn().mockImplementation((stream) => {
          pipe: jest.fn()
        }),
      }

      jest
        .spyOn(fileUploadMock, 'createReadStream')
        .mockImplementation((stream) => {
          stream.pipe()
          return Promise.resolve({
            url: 'url-result',
            path: 'file-path-result',
          })
        })

      const uploadFileResult = await myService.uploadFile(fileUploadMock)

      expect(uploadFileResult).toBeCalled()
    })
  })

This portion of your code:这部分代码:

        createReadStream: jest.fn().mockImplementation((stream) => {
          pipe: jest.fn()
        }),

Is not doing what you think it's doing.没有做你认为它正在做的事情。 You believe that the function you're passing into mockImplementation is returning an object that looks like {pipe: jest.fn()} , however, that's not what's happening.您认为您传递给 mockImplementation 的mockImplementation正在返回一个看起来像{pipe: jest.fn()}的 object ,但是,事实并非如此。 If the first thing an arrow function encounters after the arrow is an open curly bracket, that now tells TS/JS that you're inside a function body, which no longer has an implicit return.如果箭头 function 在箭头之后遇到的第一件事是打开的大括号,那么现在告诉 TS/JS 你在 function 主体内,该主体不再具有隐式返回。 Similar to if you wrote:类似于如果你写的:

// Nothing happens here, and it returns undefined
function (stream) {
  pipe: jest.fn()
}

The fix to this would be:对此的解决方法是:

(stream) => {
  return { pipe: jest.fn() };
}

Or if you wanted to preserve the succinctness of arrow operators, you just need to make sure that the first thing after the arrow isn't a curly bracket, ie:或者,如果您想保持箭头运算符的简洁性,您只需要确保箭头之后的第一件事不是大括号,即:

// Parenthesis help! 
(stream) => ({
  pipe: jest.fn()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 vue3 开玩笑 typescript 类型错误:无法读取未定义的属性“深” - jest with vue3 typescript TypeError: Cannot read property 'deep' of undefined 开玩笑:类型错误:无法读取未定义的属性 - Jest: TypeError: Cannot read property of undefined TypeError:无法读取未定义的 Jest 和 Nest 的属性“then” - TypeError: Cannot read property 'then' of undefined Jest and Nest 开玩笑:TypeError:无法读取未定义的属性“foo” - Jest: TypeError: Cannot read property 'foo' of undefined 打字稿类型错误:无法读取未定义的属性 - Typescript TypeError:Cannot read property of undefined TypeScript:TypeError:无法读取未定义的属性“ push” - TypeScript: TypeError: Cannot read property 'push' of undefined TypeError:无法读取TypeScript中未定义的属性“ currentQuestions” - TypeError: Cannot read property 'currentQuestions' of undefined in TypeScript Typescript TypeError:无法读取未定义的属性“ set” - Typescript TypeError: Cannot read property 'set' of undefined Typescript 类型错误:无法读取未定义的属性“集合” - Typescript TypeError: Cannot read property 'collection' of undefined nestjs / cqrs-TypeError:在安装时无法读取未定义的属性“值” - nestjs/cqrs - TypeError: Cannot read property 'values' of undefined on install
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM