Mock getSignedUrl from Google Storage File using Jest

Viewed 168

I have a piece of code that make use of getSignedUrl

onst [url] = await blob.getSignedUrl({ action: 'read', expires: Date.now() + 60 * 1000, contentType: mimetype })

Unfortunately, firebase emulator cannot sign the URL, my workaround is to mock the return value of blob.getSignedUrl

As following

import { File } from '@google-cloud/storage'

jest.mock('File', () => ({
  ...jest.requireActual('@google-cloud/storage'),
  getSignedUrl: jest.fn().mockReturnValue({ url: 'http://' }),
}))

Although this has no effect. How can I mock getSignedUrl function?

1 Answers

jest.mock takes the module name (or the path) as parameter.

I guess what you wanted to do is to use a spy :

import { File } from '@google-cloud/storage'

const spy = jest.spyOn(File.prototype, 'getSignedUrl')
spy.mockReturnValue({ url: 'http://' })

// if you dont need to keep spy you can do it in one line :
// jest.spyOn(File.prototype, 'getSignedUrl').mockReturnValue({ url: 'http://' })
Related